JavaScript takes an object with the same value in the array to form a new array

Source: Internet
Author: User

1 varArr=[{name:2,id:3},{name:2,id:4},{name:3,id:5},{name:3,id:6},{name:1,id:1},{name:1,id:2}];2 3 //passing in an array4 //format in a specific way5 functionSortarr (arr, str) {6     var_arr = [],7_t = [],8         //Temporary Variables9 _tmp;Ten  One     //sorting arrays by specific parameters will have the same worth of rows together Aarr = Arr.sort (function(A, b) { -         vars =A[str], -t =B[str]; the  -         returnS < T? -1:1; -     }); -  +     if(arr.length) { -_tmp = arr[0][str]; +     } A     //Console.log (arr); at     //add an object of the same category to a unified array -      for(varIincharr) { - Console.log (_tmp); -         if(Arr[i][str] = = =_tmp) { - Console.log (_tmp) - _t.push (Arr[i]); in}Else { -_tmp =Arr[i][str]; to _arr.push (_t); +_t =[Arr[i]]; -         } the     } *     //launch the final content into the new array $ _arr.push (_t);Panax Notoginseng     return_arr; - } the  +Sortarr (arr, ' name ');

JavaScript takes an object with the same value in the array to form a new array

Related Article

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.