取出一维数组中相同元素,重新组合成二维数组

goodsList = [
    {
      "groupId": "434",
      "groupName": "xxxl",
      "typeId": "136",
      "typeName": "衣服",
    },
    {
      "typeId": "136",
      "typeName": "衣服"
    },
    {
      "chargeId": "731",
      "chargeName": "xl-裤子-a",
      "typeId": "137",
    },
    {
      "groupId": "437",
      "groupName": "xxl-裤子",
      "typeId": "137",
      "typeName": "裤子"
    }
  ]


let newGoodsList = [];
let pre = {};
let curArr = [];
for(let i = 0 ; i < goodsList.length; i++) {
    let cur = goodsList[i];
     if (pre.typeId && cur.typeId == pre.typeId) {
          curArr.push(cur);
      }else {
       curArr = [cur];
       newGoodsList.push(curArr);
       pre = cur;
     }
  };

console.log(newGoodsList);

 

Guess you like

Origin blog.csdn.net/strong90/article/details/108998507