如何在每个循环中对Json对象进行分组

时间:2019-03-04 21:19:42

标签: javascript jquery arrays foreach

你好,我来自PHP背景,并试图通过Javascript / JQuery对一些JSON数据进行分组。我想通过裁剪SKU并将数据分组到一起来对数据进行分组,这是我成功完成的,但是我无法将数据放入“子项”部分,似乎只能循环第一条记录。理想情况下,子级部分应具有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等,然后子级部分具有项的所有信息,但显然在各自的组中。希望有道理!再次感谢!

2 个答案:

答案 0 :(得分:1)

您可以使用reduceproduct-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);

答案 1 :(得分:0)

尝试

restTemplate.exchange(uri, HttpMethod.POST, httpEntity, ResponseClass.class);

@RequestMapping("foo")
 public void fooMethod() {
    System.out.println("mapping = " + getMapping("fooMethod")); // you can get all methods/mapping in @PostContruct initialization phase
 }

 private String getMapping(String methodName) {
    Method methods[] = this.getClass().getMethods();
    for (int i = 0; i < methods.length; i++) {
        if (methods[i].getName() == methodName) {
            String mapping[] = methods[i].getAnnotation(RequestMapping.class).value();
            if (mapping.length > 0) {
                return mapping[mapping.length - 1];
            }
        }
    }
    return null;
}