您可以使用以下代码来实现您的需求:

const data = [
    {
        "del": 0,
        "createBy": "GW00303172",
        "createTime": "2023-10-16 11:17:16",
        "updateBy": "GW00303172",
        "updateTime": "2023-10-16 11:17:16",
        "id": 2685,
        "processId": 1383,
        "projectId": 35,
        "productCode": "PA 20230905 739d896a58",
        "productBomCode": "20230905 11 63d3244aa7",
        "materialCode": "PA 20230905 739d896a58",
        "materialName": "前衬套",
        "electrophoreticArea": 200,
        "weldBeadLength": 100,
        "roughWeight": 2,
        "materialCategory": "2",
        "materialType": 0,
        "supplierStatus": "1",
        "materialStatus": "3",
        "importanceLevel": "1",
        "sourceFrom": "0",
        "yieldCode": null,
        "level": 1,
        "weight": 1.02,
        "specifications": "--",
        "borrowRelation": "J65",
        "num": 1,
        "targetPrice": 66,
        "other": "1",
        "projectCode": "1",
        "materialId": 2685,
        "deliveryAddress": "5",
        "passRatio": "5",
        "craftCode": "GY000105",
        "craftSubType": "3",
        "type1": "0",
        "type2": "0",
        "type3": "0",
        "productionPlaceCode": "CD0003"
    },
    {
        "rowId": "1697437037701926",
        "productCode": "PA 20230905 739d896a58",
        "projectCode": "1",
        "productBomCode": "20230905 11 63d3244aa7",
        "processId": 1383,
        "materialCode": "PA 20230905 739d896a58",
        "materialName": "前衬套7",
        "materialCategory": "2",
        "specifications": "--",
        "weight": 1.02,
        "importanceLevel": "1",
        "level": 1,
        "sourceFrom": "0",
        "materialId": 2685,
        "productionPlaceCode": "CD0006",
        "deliveryAddress": "6",
        "passRatio": "5",
        "craftCode": "GY000106",
        "craftSubType": "6",
        "type1": "0",
        "type2": "1",
        "type3": "0"
    },
    // ... 其他数据项
];

const result = {};

for (let i = 0; i < data.length; i++) {
  const item = data[i];
  if (result[item.materialCode]) {
    if (result[item.materialCode][item.craftCode]) {
      result[item.materialCode][item.craftCode].push(item);
    } else {
      result[item.materialCode][item.craftCode] = [item];
    }
  } else {
    result[item.materialCode] = {
      [item.craftCode]: [item],
    };
  }
}

for (const materialCode in result) {
  for (const craftCode in result[materialCode]) {
    console.log(result[materialCode][craftCode]);
  }
}

这段代码会先将数组中相同 materialCode 的数据进行分组,然后再根据 craftCode 进行分组。最后,会打印出每个相同 craftCode 的数据。


原文地址: http://www.cveoy.top/t/topic/pi8M 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录