An approach to the algorithm of JavaScript (jquery) array de-weight

Source: Internet
Author: User

          the method is ingenious but to know beforehand that the corresponding name or other property names primary key values only apply to known data objects for invocation;var arr1 = [{ name:  ' C30 '  }, { name:  ' C35 '  }, {  name:  ' C40 '  }, { name:  ' C45 '  }, { name:  ' C50 '  }, {  name:  ' C60 '  }, { name:  ' C55 '}];//take the list in strength settings var arr2 = [{  name:  ' C30 '  }, { name:  ' C35 '}];//take to the list of current contract details var temp = [];  Temporary array 1  var temparray = []; //temporary array 2 for  (var i = 0;  i < arr2.length; i++)  {    temp[arr2[i].name] =  true; //Ingenious Place: The property value corresponding to the object of array B as the key of the temporary array 1 and assigned a value of true   };for  (var i = 0; i  < arr1.length; i++)  {    if  (!temp[arr1[i].name])  {         temparray.push (Arr1[i])  //Clever place: At the same time the array a object corresponding to the property value as a temporary array 1 key and determine whether it is true, If you don't repeat for true, merge into a new array so you can get a newThere is no duplicate array       };};/ / document.write (Temparray.join (",")  +  ""); Console.log (temparray);   //  var datalist=[{name: ' C30 '},{name: ' C35 '},{name: ' C40 '},{name: ' C45 '},{name: ' C50 '},{name: ' C60 '},{ Name: ' C55 '}];      //to the list in strength settings      var rowlist=[{name: ' C30 '},{ Name: ' C35 '}];     //take the list of current contract details       function  Remove (Arrperson,objpropery,objvalue) {        return $.grep ( Arrperson, function (cur,i) {         return cur[ objpropery]!=objvalue;      });    }     for (var i = 0; i < datalist.length; i++) {         //all the data to remove the existing data         //can consider replacing more efficient algorithms for removal          for (var j =0;j < rowlist.length; j++)  {            if  (datalist[i].name== Rowlist[j].name) {&NBSP;&NBSP;&NBSP;&NBsp;            //remove the data from the corresponding array                  //datalist.pop (DataList[i].name) ;                 datalist  = remove (DataList, "name", Rowlist[j].name);             }        }     }            a+b      js How to merge two arrays and remove duplicates Var a = [1,2,3,4,5];var b  = [1,2,4,5];alert (A, B); //  displays the corresponding de-weight array function tab (ARR1,ARR2) {     var arr = arr1.concat (ARR2);     var lastarr = [] ;     for (var i = 0;i<arr.length;i++)     {         if (! unique (Arr[i],lastarr))          {            lastarr.push (Arr[i] );         }    }    return  lastarr;} Function unique (N,arr) {    for (var i=0;i<arr.length;i++)      {        if (N==arr[i]) {             return true;        }    }     return false;}               

JavaScript (jquery) array de-weight algorithm discussion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.