如何在每个循环中对 Json 对象进行分组
Posted
技术标签:
【中文标题】如何在每个循环中对 Json 对象进行分组【英文标题】:How To Group Json Objects In Each Loop 【发布时间】:2019-07-26 06:22:55 【问题描述】:您好,我来自 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 等,然后子部分包含项目的所有信息,但显然在各自的组中。希望这是有道理的!再次感谢!
【问题讨论】:
你能提供一个预期输出的例子吗?请在问题中 【参考方案1】:您可以使用reduce
按product-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);
【讨论】:
请打开开发者工具并查看控制台【参考方案2】:试试
items.forEach(x =>
let k=x.product_sku.replace(/-[^-]*?$/,'');
let g=groupedItems[k]||[];
g.push(x);
groupedItems[k]=g;
)
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.forEach(x =>
let k=x.product_sku.replace(/-[^-]*?$/,'');
let g=groupedItems[k]||[];
g.push(x);
groupedItems[k]=g;
)
console.log(groupedItems);
【讨论】:
以上是关于如何在每个循环中对 Json 对象进行分组的主要内容,如果未能解决你的问题,请参考以下文章