简体   繁体   English

JavaScript 将数据分组到 2 层深的树视图中

[英]JavaScript group data into tree view 2 levels deep

I have a JSON object like this:我有一个像这样的 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
        }
    ]
};

I want to make this into a new JSON object where everything is grouped by id (it already is but I need the number to become the key) then by categoryCode so the result looks something like this:我想将它变成一个新的 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
        }
    }
}

Is there a simple way to do this either using lodash or just plain JavaScript or some NPM package?有没有一种简单的方法可以使用 lodash 或仅使用普通的 JavaScript 或一些 NPM 包来做到这一点?

I create a general function that receiving array of items and parse it based on attrKeys and infoKeys array:我创建了一个接收项目数组的通用函数,并根据 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})

Follow the nested for...of loop approach:遵循嵌套的for...of循环方法:

 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 }] }]; let finalData = {}; for (let obj of data) { finalData[obj.id] = {}; for (var item of obj.items) { finalData[obj.id][item.categoryCode] = { ...item }; } } console.log(finalData);

You can use Array.reduce function to achieve it.您可以使用 Array.reduce 函数来实现它。

 const array = [{ 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: [ { id: '3', saleId: '123', saleIdAndItemId: '123456', locationId: '123', itemCode: '456', itemDescription: 'my item', categoryCode: '557', categoryDescription: 'my category 7', qty: 10, saleValue: 200 } ] }]; console.log( array // flatten the items array .reduce((prev, curr) => { return prev.concat(curr.items); }, []) .reduce((prev, curr) => { if (!prev[curr.id]) prev[curr.id] = {}; if (!prev[curr.id][curr.categoryCode]) prev[curr.id][curr.categoryCode] = {}; prev[curr.id][curr.categoryCode] = curr; return prev; }, {}) );

Alternatively, you can archive that by just 7 lines of code using reduce function.或者,您可以使用 reduce 函数仅通过 7 行代码将其存档。

 const array = [ { 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 } ] } ]; console.log( array.reduce((prev, curr) => { prev[curr.id] = curr.items.reduce((prev, curr) => { (prev[curr.categoryCode] = []).push(curr); return prev; }, {}); return prev; }, {}) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM