| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 
 | let arr = [{
 "id": 577,
 "name": "艾杜纱 毛孔洁净洗面奶",
 "skuName": "125mL",
 "image": "commodityImage/haZW5gLF.jpg",
 "price": 122.32,
 "store": 327,
 "status": 1,
 "brandId": 18,
 "categoryId": 32
 }, {
 "id": 536,
 "name": "心机彩妆 星魅霓光唇膏",
 "skuName": "BE300",
 "image": "commodityImage/ibg54OOx.jpg",
 "price": 273.65,
 "store": 50,
 "status": 1,
 "brandId": 17,
 "categoryId": 33
 }, {
 "id": 546,
 "name": "心机彩妆 炫眉膏",
 "skuName": "77",
 "image": "commodityImage/356LBmxe.jpg",
 "price": 160.68,
 "store": 20,
 "status": 1,
 "brandId": 17,
 "categoryId": 20
 }
 ]
 
 console.log(arr,'原始数组');
 console.log(sortArr(arr, 'brandId'),'转化后的数组');
 
 
 
 
 
 function sortArr(arr, key) {
 let newArr = [],
 tempArr = [],
 temp;
 
 
 arr = arr.sort(function(a, b) {
 let s = a[key],
 t = b[key];
 
 return s < t ? -1 : 1;
 });
 console.log(arr,"排序之后的数组");
 
 if ( arr.length ){
 temp = arr[0][key];
 }
 
 
 for (let i in arr) {
 if ( arr[i][key] === temp ){
 tempArr.push( arr[i] );
 } else {
 temp = arr[i][key];
 newArr.push(tempArr);
 tempArr = [arr[i]];
 }
 }
 
 newArr.push(tempArr);
 return newArr;
 }
 
 
 |