gpt4 book ai didi

JavaScript 将数据分组到 2 层深度的 TreeView 中

转载 作者:行者123 更新时间:2023-12-02 22:16:38 25 4
gpt4 key购买 nike

我有一个像这样的 JSON 对象:

{
id: '1',
items: [
{
id: '1',
saleId: '123',
saleIdAndItemId: '123456',
locationId: '123',
itemCode: '456',
itemDescription: 'my item',
categoryCode: '555',
categoryDescription: 'my category',
qty: 10,
saleValue: 200
},
{
id: '1',
saleId: '123',
saleIdAndItemId: '123456',
locationId: '123',
itemCode: '456',
itemDescription: 'my item',
categoryCode: '555',
categoryDescription: 'my category',
qty: 10,
saleValue: 200
}
]
},
{
id: '2',
items: [
{
id: '2',
saleId: '123',
saleIdAndItemId: '123456',
locationId: '123',
itemCode: '456',
itemDescription: 'my item',
categoryCode: '556',
categoryDescription: 'my category 6',
qty: 10,
saleValue: 200
}
]
},
{
id: '3',
items: [
{
venueId: '3',
saleId: '123',
saleIdAndItemId: '123456',
locationId: '123',
itemCode: '456',
itemDescription: 'my item',
categoryCode: '557',
categoryDescription: 'my category 7',
qty: 10,
saleValue: 200
}
]
};

我想把它变成一个新的 JSON 对象,其中所有内容都按 id 分组(它已经是,但我需要数字成为键),然后按categoryCode分组,所以结果看起来像这样:

{
'1': {
'555': {
id: '1',
saleId: '123',
saleIdAndItemId: '123456',
locationId: '123',
itemCode: '456',
itemDescription: 'my item',
categoryCode: '555',
categoryDescription: 'my category',
qty: 10,
saleValue: 200
}
}
}

是否有一种简单的方法可以使用 lodash 或仅使用纯 JavaScript 或某些 NPM 包来执行此操作?

最佳答案

我创建了一个通用函数,用于接收项目数组并根据 attrKeys 和 infoKeys 数组解析它:

const data = [{
id: '1',
items: [{
id: '1',
saleId: '123',
saleIdAndItemId: '123456',
locationId: '123',
itemCode: '456',
itemDescription: 'my item',
categoryCode: '555',
categoryDescription: 'my category',
qty: 10,
saleValue: 200
}, {
id: '1',
saleId: '123',
saleIdAndItemId: '123456',
locationId: '123',
itemCode: '456',
itemDescription: 'my item',
categoryCode: '555',
categoryDescription: 'my category',
qty: 10,
saleValue: 200
}]
}, {
id: '2',
items: [{
id: '2',
saleId: '123',
saleIdAndItemId: '123456',
locationId: '123',
itemCode: '456',
itemDescription: 'my item',
categoryCode: '556',
categoryDescription: 'my category 6',
qty: 10,
saleValue: 200
}]
}, {
id: '3',
items: [{
venueId: '3',
saleId: '123',
saleIdAndItemId: '123456',
locationId: '123',
itemCode: '456',
itemDescription: 'my item',
categoryCode: '557',
categoryDescription: 'my category 7',
qty: 10,
saleValue: 200
}]
}];

const transformData = (input, attrKeys = [], infoKeys = [], deep = 0) => {
if (input && typeof input === 'object' && input.length) {
const key1 = attrKeys[deep];
const key2 = infoKeys[deep];
deep++;
const output = {}

input.forEach(i => {
const info = i[key2] ? transformData(i[key2], attrKeys, infoKeys, deep) : i;

output[`${i[key1]}`] = info;
});

return output;
}

return input;
};

//What you want is here
const transformedData = transformData(data, ['id','categoryCode'], ['items'])
console.log({transformedData})

关于JavaScript 将数据分组到 2 层深度的 TreeView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59368892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com