我有一個物件陣列,我想將它們轉換為使用圖表中的資料。有人推薦使用 lodash,但我不想使用任何庫。所以這里是陣列的例子:
const items = [
{
priceChangeType: 'CL',
hierarchy: {
department: {
description: 'TEXTILES',
},
},
},
{
priceChangeType: 'PM',
hierarchy: {
department: {
description: 'CLOTHES',
},
},
},
{
priceChangeType: 'CL',
hierarchy: {
department: {
description: 'TEXTILES',
},
},
},
{
priceChangeType: 'CL',
hierarchy: {
department: {
description: 'CLOTHES',
},
},
},
{
priceChangeType: 'PM',
hierarchy: {
department: {
description: 'BATH',
},
},
},
{
priceChangeType: 'PM',
hierarchy: {
department: {
description: 'TOOLS',
},
},
},
{
priceChangeType: 'CL',
hierarchy: {
department: {
description: 'TOOLS',
},
},
},
{
priceChangeType: 'CL',
hierarchy: {
department: {
description: 'TOOLS',
},
},
},
]
我想要這樣的輸出,這是圖表所需的格式。
const data = [
{name: 'TOOLS', PM: 1, CL: 2},
{name: 'CLOTHES', PM: 1, CL: 1},
{name: 'TEXTILES', PM: 0, CL: 2},
{name: 'BATH', PM: 1, CL: 0},
]
這是我來得最遠的地方,但只是總數。
const totalPriceChangesType = Object.entries(items.reduce((r, v, i, a, k = v.priceChangeType) => ((r[k] || (r[k] = [])).push(v), r), {})).map(
([key, value]) => ({
[key] : value.length,
}),
)
uj5u.com熱心網友回復:
可以創建一個物件(priceChangeTypes所有的)priceChangeType由陣列映射到初始化為0[priceChangeType, 0]對,并使用Object.fromEntries()。
然后將原始陣列縮減為 Map。對于每個新物件,hierarchy.department.description通過傳播priceChangeTypes到一個新物件在 Map 中創建一個條目,并增加相關的priceChangeType.
使用 轉換回陣列Array.from()。
const items = [{priceChangeType: 'CL',hierarchy: {department: {description: 'TEXTILES'}}},{priceChangeType: 'PM',hierarchy: {department: {description: 'CLOTHES'}}},{priceChangeType: 'CL',hierarchy: {department: {description: 'TEXTILES'}}},{priceChangeType: 'CL',hierarchy: {department: {description: 'CLOTHES'}}},{priceChangeType: 'PM',hierarchy: {department: {description: 'BATH'}}},{priceChangeType: 'PM',hierarchy: {department: {description: 'TOOLS'}}},{priceChangeType: 'CL',hierarchy: {department: {description: 'TOOLS'}}},{priceChangeType: 'CL',hierarchy: {department: {description: 'TOOLS'}}}]
// Create an initialized counts object = { CL: 0, PM: 0 }
const priceChangeTypes = Object.fromEntries(items.map(o => [o.priceChangeType, 0]))
const result = Array.from(
items.reduce((acc, o) => {
const key = o.hierarchy.department.description
// initialize the object for the current key if needed
if(!acc.has(key)) acc.set(key, { ...priceChangeTypes })
// increment the relevant priceChangeType
acc.get(key)[o.priceChangeType] = 1
return acc
}, new Map()),
([names, values]) => ({ name, ...values }) // convert to an array
)
console.log(result)
uj5u.com熱心網友回復:
也許這個例子可以幫助你?
const items = [{
priceChangeType: "CL",
hierarchy: {
department: {
description: "TEXTILES"
}
}
},
{
priceChangeType: "PM",
hierarchy: {
department: {
description: "CLOTHES"
}
}
},
{
priceChangeType: "CL",
hierarchy: {
department: {
description: "TEXTILES"
}
}
},
{
priceChangeType: "CL",
hierarchy: {
department: {
description: "CLOTHES"
}
}
},
{
priceChangeType: "PM",
hierarchy: {
department: {
description: "BATH"
}
}
},
{
priceChangeType: "PM",
hierarchy: {
department: {
description: "TOOLS"
}
}
},
{
priceChangeType: "CL",
hierarchy: {
department: {
description: "TOOLS"
}
}
},
{
priceChangeType: "CL",
hierarchy: {
department: {
description: "TOOLS"
}
}
}
];
// collect all possible PM, CL, etc. (example: {PM:0, CL:0})
const priceChangeTypes = items.reduce((acc, item) => (acc[item.priceChangeType] = 0, acc), {});
const total = Object.values(items.reduce((acc, item) => {
const descriprion = item.hierarchy.department.description;
const priceChangeType = item.priceChangeType;
// create a new base object {name: "{description}", PM:0, CL:0, ...}
if (!acc[descriprion]) acc[descriprion] = {
name: descriprion,
...priceChangeTypes
};
acc[descriprion][priceChangeType] ;
return acc;
}, {}));
console.log(total);
uj5u.com熱心網友回復:
邏輯
items使用Array.map和從陣列生成唯一型別Setitems根據名稱和型別減少陣列- 將缺少的型別添加到
output陣列中的各個節點
const items = [{priceChangeType: 'CL',hierarchy: {department: {description: 'TEXTILES'}}},{priceChangeType: 'PM',hierarchy: {department: {description: 'CLOTHES'}}},{priceChangeType: 'CL',hierarchy: {department: {description: 'TEXTILES'}}},{priceChangeType: 'CL',hierarchy: {department: {description: 'CLOTHES'}}},{priceChangeType: 'PM',hierarchy: {department: {description: 'BATH'}}},{priceChangeType: 'PM',hierarchy: {department: {description: 'TOOLS'}}},{priceChangeType: 'CL',hierarchy: {department: {description: 'TOOLS'}}},{priceChangeType: 'CL',hierarchy: {department: {description: 'TOOLS'}}}];
// Generate unique types
const types = Array.from(new Set(items.map(item => item.priceChangeType)));
// Reduce the items array against the name and type
const output = items.reduce((acc, curr, index, array, name = curr.hierarchy.department.description, type = curr.priceChangeType) => {
acc[name] = acc[name] || {};
acc[name][type] = acc[name][type] || 1;
return acc;
}, {});
// Add the missing type to each object in output
Object.entries(output).forEach(([key, value]) => types.forEach(type => output[key][type] = output[key][type] || 0))
console.log(output);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/404632.html
標籤:
