JS Array Reduce 以计算具有多个匹配键的对象值
JS Array Reduce to count Object Values with a Multiple Matching Keys
我有一个这样的数组:
var objects = [{
cat: "Fixtures"
name: "brilliance_denali_(best)"
shadowColor: "warmWhite"
subCat: "Spot & Bullet Lights"
title: "Brilliance Denali (Best)"
wattage: {upPrice: 10, value: '10W'}
},
{
cat: "Fixtures"
name: "brilliance_denali_(best)"
shadowColor: "warmWhite"
subCat: "Spot & Bullet Lights"
title: "Brilliance Denali (Best)"
wattage: {upPrice: 10, value: '10W'}
},
{
cat: "Fixtures"
name: "brilliance_denali_(best)"
shadowColor: "red"
subCat: "Spot & Bullet Lights"
title: "Brilliance Denali (Best)"
wattage: {upPrice: 12, value: '12W'}
},
{
cat: "Fixtures"
name: "bullet_lights"
shadowColor: "blue"
subCat: "Lights"
title: "Bullet Lights"
wattage: {upPrice: 28, value: '10W'}
}]
我想根据“cat”减少这个数组,然后在 cat 里面是“subCat”,然后在我想用额外的“COUNT”键减少它们的对象中,如果它们具有相同的“名称” ,“shadowColor”和“瓦数值”。所以总的来说,输出数组应该是这样的:
var formattedObjects = [
{
cat: 'Fixtures',
children: [
{
subCat: 'Lights',
childres: [
{
cat: "Fixtures"
name: "bullet_lights"
shadowColor: "blue"
subCat: "Lights"
title: "Bullet Lights"
wattage: {upPrice: 28, value: '10W'},
count: 1 <-- COUNT BASED ON SAME : NAME && SHADOWCOLOR && WATTAGE VALUE
}
]
}, {
subCat: 'Spot & Bullet Lights',
childres: [
{
{
cat: "Fixtures"
name: "brilliance_denali_(best)"
shadowColor: "warmWhite"
subCat: "Spot & Bullet Lights"
title: "Brilliance Denali (Best)"
wattage: {upPrice: 10, value: '10W'},
count: 2 <-- COUNT BASED ON SAME : NAME && SHADOWCOLOR && WATTAGE VALUE
},
{
cat: "Fixtures"
name: "brilliance_denali_(best)"
shadowColor: "red"
subCat: "Spot & Bullet Lights"
title: "Brilliance Denali (Best)"
wattage: {upPrice: 12, value: '12W'},
count: 1 <-- COUNT BASED ON SAME : NAME && SHADOWCOLOR && WATTAGE VALUE
}
}
]
}
]
}
]
到目前为止,我能够基于“subCat”和“cat”减少它,每个都有嵌套的子项,但我不确定我应该如何在其中添加计数并删除具有相同“名称”的对象,“ sahdowColor”和“瓦数值”。这是我的减速器在没有计数的情况下的样子:
const formatedObjects = objects.reduce((list, item) => {
const { cat, subCat } = item;
const hasList = !!list?.[cat];
const subList = list?.[cat]?.children.find(child => child.subCat === subCat)
const value = {
name: item.name,
title: item.title,
shadowColor: item.shadowColor ? item.shadowColor : null ,
wattage: item.wattage ? item.wattage : null
}; // the element in data property
if (!hasList) {
list[cat] = { cat, children: [
{
subCat,
children: [value]
}
]}
} else if (!subList){
list[cat].children.push({ subCat, children: [value]})
} else {
subList.children.push(value)
}
return list;
}, {})
知道我应该如何在那个减速器中添加计数吗???谢谢
不确定,但这就是您要找的东西吗?
let objects = [{ cat: "Fixtures", name: "brilliance_denali_(best)", shadowColor: "warmWhite", subCat: "Spot & Bullet Lights", title: "Brilliance Denali (Best)", wattage: { upPrice: 10, value: '10W' } }, { cat: "Fixtures", name: "brilliance_denali_(best)", shadowColor: "warmWhite", subCat: "Spot & Bullet Lights", title: "Brilliance Denali (Best)", wattage: { upPrice: 10, value: '10W' } }, { cat: "Fixtures", name: "brilliance_denali_(best)", shadowColor: "red", subCat: "Spot & Bullet Lights", title: "Brilliance Denali (Best)", wattage: { upPrice: 12, value: '12W' } }, { cat: "Fixtures", name: "bullet_lights", shadowColor: "blue", subCat: "Lights", title: "Bullet Lights", wattage: { upPrice: 28, value: '10W' } }]
let result = [], counter = {};
for (let item of objects) {
let countId = item.name + item.shadowColor + item.wattage.value
if (counter[countId]) {
counter[countId].count++;
continue
}
let cat = result.find(o => o.cat == item.cat);
if (!cat) result.push(cat = { cat: item.cat, children: [] });
let subCat = cat.children.find(o => o.subCat == item.subCat);
if (!subCat) cat.children.push(subCat = { subCat: item.subCat, childres: [] });
item.count = 1
subCat.childres.push(counter[countId] = item);
}
console.log(result)
我有一个这样的数组:
var objects = [{
cat: "Fixtures"
name: "brilliance_denali_(best)"
shadowColor: "warmWhite"
subCat: "Spot & Bullet Lights"
title: "Brilliance Denali (Best)"
wattage: {upPrice: 10, value: '10W'}
},
{
cat: "Fixtures"
name: "brilliance_denali_(best)"
shadowColor: "warmWhite"
subCat: "Spot & Bullet Lights"
title: "Brilliance Denali (Best)"
wattage: {upPrice: 10, value: '10W'}
},
{
cat: "Fixtures"
name: "brilliance_denali_(best)"
shadowColor: "red"
subCat: "Spot & Bullet Lights"
title: "Brilliance Denali (Best)"
wattage: {upPrice: 12, value: '12W'}
},
{
cat: "Fixtures"
name: "bullet_lights"
shadowColor: "blue"
subCat: "Lights"
title: "Bullet Lights"
wattage: {upPrice: 28, value: '10W'}
}]
我想根据“cat”减少这个数组,然后在 cat 里面是“subCat”,然后在我想用额外的“COUNT”键减少它们的对象中,如果它们具有相同的“名称” ,“shadowColor”和“瓦数值”。所以总的来说,输出数组应该是这样的:
var formattedObjects = [
{
cat: 'Fixtures',
children: [
{
subCat: 'Lights',
childres: [
{
cat: "Fixtures"
name: "bullet_lights"
shadowColor: "blue"
subCat: "Lights"
title: "Bullet Lights"
wattage: {upPrice: 28, value: '10W'},
count: 1 <-- COUNT BASED ON SAME : NAME && SHADOWCOLOR && WATTAGE VALUE
}
]
}, {
subCat: 'Spot & Bullet Lights',
childres: [
{
{
cat: "Fixtures"
name: "brilliance_denali_(best)"
shadowColor: "warmWhite"
subCat: "Spot & Bullet Lights"
title: "Brilliance Denali (Best)"
wattage: {upPrice: 10, value: '10W'},
count: 2 <-- COUNT BASED ON SAME : NAME && SHADOWCOLOR && WATTAGE VALUE
},
{
cat: "Fixtures"
name: "brilliance_denali_(best)"
shadowColor: "red"
subCat: "Spot & Bullet Lights"
title: "Brilliance Denali (Best)"
wattage: {upPrice: 12, value: '12W'},
count: 1 <-- COUNT BASED ON SAME : NAME && SHADOWCOLOR && WATTAGE VALUE
}
}
]
}
]
}
]
到目前为止,我能够基于“subCat”和“cat”减少它,每个都有嵌套的子项,但我不确定我应该如何在其中添加计数并删除具有相同“名称”的对象,“ sahdowColor”和“瓦数值”。这是我的减速器在没有计数的情况下的样子:
const formatedObjects = objects.reduce((list, item) => {
const { cat, subCat } = item;
const hasList = !!list?.[cat];
const subList = list?.[cat]?.children.find(child => child.subCat === subCat)
const value = {
name: item.name,
title: item.title,
shadowColor: item.shadowColor ? item.shadowColor : null ,
wattage: item.wattage ? item.wattage : null
}; // the element in data property
if (!hasList) {
list[cat] = { cat, children: [
{
subCat,
children: [value]
}
]}
} else if (!subList){
list[cat].children.push({ subCat, children: [value]})
} else {
subList.children.push(value)
}
return list;
}, {})
知道我应该如何在那个减速器中添加计数吗???谢谢
不确定,但这就是您要找的东西吗?
let objects = [{ cat: "Fixtures", name: "brilliance_denali_(best)", shadowColor: "warmWhite", subCat: "Spot & Bullet Lights", title: "Brilliance Denali (Best)", wattage: { upPrice: 10, value: '10W' } }, { cat: "Fixtures", name: "brilliance_denali_(best)", shadowColor: "warmWhite", subCat: "Spot & Bullet Lights", title: "Brilliance Denali (Best)", wattage: { upPrice: 10, value: '10W' } }, { cat: "Fixtures", name: "brilliance_denali_(best)", shadowColor: "red", subCat: "Spot & Bullet Lights", title: "Brilliance Denali (Best)", wattage: { upPrice: 12, value: '12W' } }, { cat: "Fixtures", name: "bullet_lights", shadowColor: "blue", subCat: "Lights", title: "Bullet Lights", wattage: { upPrice: 28, value: '10W' } }]
let result = [], counter = {};
for (let item of objects) {
let countId = item.name + item.shadowColor + item.wattage.value
if (counter[countId]) {
counter[countId].count++;
continue
}
let cat = result.find(o => o.cat == item.cat);
if (!cat) result.push(cat = { cat: item.cat, children: [] });
let subCat = cat.children.find(o => o.subCat == item.subCat);
if (!subCat) cat.children.push(subCat = { subCat: item.subCat, childres: [] });
item.count = 1
subCat.childres.push(counter[countId] = item);
}
console.log(result)