javascript – 如何在每个循环中组合Json对象

您好我来自php背景并尝试通过
Javascript / JQuery对一些JSON数据进行分组.我想通过裁剪SKU并将记录分组到我已经成功完成的数据来对数据进行分组,但是我无法将数据输入到’children’部分,它似乎只循环第一条记录.理想情况下,children部分应该具有JSON文件中的所有数据,但是由父SKU对它们进行分组.

我尝试过以下方法:

var items = [
  {
    product_type: "configurable",
    options: [
      {
        label: "Size",
        value: "7.5",
        option_id: 190,
        option_value: 106
      }
    ],
    qty: 3,
    item_id: "300",
    product_id: "88105",
    product_name: "MENS SHOE TUDOR BLACK",
    product_sku: "M1S-TUD02-AVEL-79-7.5",
  },
  {
    product_type: "configurable",
    options: [
      {
        label: "Size",
        value: "11",
        option_id: 190,
        option_value: 99
      }
    ],
    qty: 3,
    item_id: "298",
    product_id: "88105",
    product_name: "MENS SHOE TUDOR BLACK",
    product_sku: "M1S-TUD02-AVEL-79-11",
  },
  {
    product_type: "configurable",
    options: [
      {
        label: "Size",
        value: "M",
        option_id: 190,
        option_value: 66
      }
    ],
    qty: 1,
    item_id: "111",
    product_id: "99945",
    product_name: "WOMENS SWEATER",
    product_sku: "W1R-613K-A10400-96-M",
  },
  {
    product_type: "configurable",
    options: [
      {
        label: "Size",
        value: "L",
        option_id: 190,
        option_value: 12
      }
    ],
    qty: 20,
    item_id: "278",
    product_id: "99945",
    product_name: "WOMENS SWEATER",
    product_sku: "W1R-613K-A10400-96-L",
  },
];

var groupedItems = {};
$.each(items, function(key, item) {
  if (item.product_type === "configurable") {
    var sku = item.product_sku,
      parentSku = sku.replace(sku.substring(sku.lastIndexOf("-")), "");

    if (sku.indexOf(parentSku) >= 0) {
      groupedItems[parentSku] = {
        product_sku: parentSku,
        product_name: item.product_name,
        product_type: item.product_type,
        children: [item]
      };
    }
  }
});
console.log(groupedItems);
<!DOCTYPE html>
<html>
  <head>
    <title>Parcel Sandbox</title>
    <meta charset="UTF-8" />
  </head>

  <body>
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
  </body>
</html>

任何帮助都将受到欢迎.我想要一个数组,它将数据分组到各自的组中,并从第一个记录中获得一些信息,如名称,父sku等,然后子部分具有项目的所有信息,但显然在各自的组中.希望它有意义!再次感谢!

最佳答案 您可以使用reduce来按产品sku分组

var items = [{
    product_type: "configurable",
    options: [{
      label: "Size",
      value: "7.5",
      option_id: 190,
      option_value: 106
    }],
    qty: 3,
    item_id: "300",
    product_id: "88105",
    product_name: "MENS SHOE TUDOR BLACK",
    product_sku: "M1S-TUD02-AVEL-79-7.5",
  },
  {
    product_type: "configurable",
    options: [{
      label: "Size",
      value: "11",
      option_id: 190,
      option_value: 99
    }],
    qty: 3,
    item_id: "298",
    product_id: "88105",
    product_name: "MENS SHOE TUDOR BLACK",
    product_sku: "M1S-TUD02-AVEL-79-11",
  },
  {
    product_type: "configurable",
    options: [{
      label: "Size",
      value: "M",
      option_id: 190,
      option_value: 66
    }],
    qty: 1,
    item_id: "111",
    product_id: "99945",
    product_name: "WOMENS SWEATER",
    product_sku: "W1R-613K-A10400-96-M",
  },
  {
    product_type: "configurable",
    options: [{
      label: "Size",
      value: "L",
      option_id: 190,
      option_value: 12
    }],
    qty: 20,
    item_id: "278",
    product_id: "99945",
    product_name: "WOMENS SWEATER",
    product_sku: "W1R-613K-A10400-96-L",
  },
];

var groupedItems = items.reduce((acc, item) => {
  if (item.product_type === "configurable") {
    var sku = item.product_sku;
    var parentSku = sku.replace(sku.substring(sku.lastIndexOf("-")), "");
		
    if (sku.indexOf(parentSku) >= 0) {
      if (!!acc[parentSku]) {
        let children = acc[parentSku].children;
        acc[parentSku].children = [...children, { ...item
        }];
      } else {
        acc[parentSku] = {
          product_sku: parentSku,
          product_name: item.product_name,
          product_type: item.product_type,
          children: [item]
        };
      }
    }
  }

  return acc;
}, []);

console.log(groupedItems);
点赞