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