如何获取嵌套数组中所有同名字段的总和?
How to the get the sum of all the field with the same name in a nested array?
我试图找到嵌套数组的所有字段值的总和,我不擅长解释这样的东西,我希望下面的代码能让你对我想做的事情有所了解。
数组看起来像这样
data = [
{
id: 8434,
name: listName,
data: [
{name: "a name",
quantity: 20,
}
{name: "another name",
quantity: 40,
}
{name: "another new name",
quantity: 40,
}
]
},
{
id: 54343,
name: "new list name",
data: [
{name: "a name",
quantity: 12,
}
{name: "another name",
quantity: 10,
}
{name: "another new name",
quantity: 16,
}
]
}
]
这就是我希望数据在执行总和后的样子
transformed = [
{name: "a name", quantity: 32},
{name: "another name", quantity: 50},
{name: "another new name", quantity: 56}
]
const transform = (data) => {
const quantitySum = new Map()
data.forEach(list => {
list.data.forEach(({ name, quantity }) => {
if (quantitySum.has(name)) {
quantitySum.set(name, quantitySum.get(name) + quantity)
} else {
quantitySum.set(name, quantity)
}
})
})
return Array.from(quantitySum.entries())
.map(([name, sum]) => ({
name,
quantity: sum
}))
}
您可以使用 Array.flatMap()
后跟 Array.reduce()
以获得所需的结果。
我们首先调用 .flatMap() 获取所有数据值,然后对这些值调用 .reduce() 以 name
.
对每个值求和
这将创建一个对象,每个名称都有一个属性,然后我们调用Object.values()
到return到一个数组。
const data = [ { id: 8434, name: 'listName', data: [ { name: "a name", quantity: 20, }, { name: "another name", quantity: 40, }, { name: "another new name", quantity: 40, } ] }, { id: 54343, name: "new list name", data: [ { name: "a name", quantity: 12, }, { name: "another name", quantity: 10, }, { name: "another new name", quantity: 16, } ] } ];
const transformed = Object.values(data.flatMap(({ data }) => data).reduce((acc, { name , quantity }) => {
acc[name] = acc[name] || { name, quantity: 0 };
acc[name].quantity += quantity;
return acc;
}, {}))
console.log('Transformed:', transformed)
.as-console-wrapper { max-height: 100% !important; }
只需将它们循环并创建一个新对象..
const data = [
{
id: 8434,
name: 'listName',
data: [
{
name: "a name",
quantity: 20,
},
{
name: "another name",
quantity: 40,
},
{
name: "another new name",
quantity: 40,
}
]
},
{
id: 54343,
name: "new list name",
data: [
{
name: "a name",
quantity: 12,
},
{
name: "another name",
quantity: 10,
},
{
name: "another new name",
quantity: 16,
}
]
}
]
// Logic:
const transformed = []
data.forEach(d => {
d.data.forEach(item => {
const exist = transformed.find(t => t.name == item.name)
if(exist)
exist.quantity += item.quantity
else
transformed.push(item)
})
})
console.log(transformed)
输出:
[
{ name: 'a name', quantity: 32 },
{ name: 'another name', quantity: 50 },
{ name: 'another new name', quantity: 56 }
]
我试图找到嵌套数组的所有字段值的总和,我不擅长解释这样的东西,我希望下面的代码能让你对我想做的事情有所了解。
数组看起来像这样
data = [
{
id: 8434,
name: listName,
data: [
{name: "a name",
quantity: 20,
}
{name: "another name",
quantity: 40,
}
{name: "another new name",
quantity: 40,
}
]
},
{
id: 54343,
name: "new list name",
data: [
{name: "a name",
quantity: 12,
}
{name: "another name",
quantity: 10,
}
{name: "another new name",
quantity: 16,
}
]
}
]
这就是我希望数据在执行总和后的样子
transformed = [
{name: "a name", quantity: 32},
{name: "another name", quantity: 50},
{name: "another new name", quantity: 56}
]
const transform = (data) => {
const quantitySum = new Map()
data.forEach(list => {
list.data.forEach(({ name, quantity }) => {
if (quantitySum.has(name)) {
quantitySum.set(name, quantitySum.get(name) + quantity)
} else {
quantitySum.set(name, quantity)
}
})
})
return Array.from(quantitySum.entries())
.map(([name, sum]) => ({
name,
quantity: sum
}))
}
您可以使用 Array.flatMap()
后跟 Array.reduce()
以获得所需的结果。
我们首先调用 .flatMap() 获取所有数据值,然后对这些值调用 .reduce() 以 name
.
这将创建一个对象,每个名称都有一个属性,然后我们调用Object.values()
到return到一个数组。
const data = [ { id: 8434, name: 'listName', data: [ { name: "a name", quantity: 20, }, { name: "another name", quantity: 40, }, { name: "another new name", quantity: 40, } ] }, { id: 54343, name: "new list name", data: [ { name: "a name", quantity: 12, }, { name: "another name", quantity: 10, }, { name: "another new name", quantity: 16, } ] } ];
const transformed = Object.values(data.flatMap(({ data }) => data).reduce((acc, { name , quantity }) => {
acc[name] = acc[name] || { name, quantity: 0 };
acc[name].quantity += quantity;
return acc;
}, {}))
console.log('Transformed:', transformed)
.as-console-wrapper { max-height: 100% !important; }
只需将它们循环并创建一个新对象..
const data = [
{
id: 8434,
name: 'listName',
data: [
{
name: "a name",
quantity: 20,
},
{
name: "another name",
quantity: 40,
},
{
name: "another new name",
quantity: 40,
}
]
},
{
id: 54343,
name: "new list name",
data: [
{
name: "a name",
quantity: 12,
},
{
name: "another name",
quantity: 10,
},
{
name: "another new name",
quantity: 16,
}
]
}
]
// Logic:
const transformed = []
data.forEach(d => {
d.data.forEach(item => {
const exist = transformed.find(t => t.name == item.name)
if(exist)
exist.quantity += item.quantity
else
transformed.push(item)
})
})
console.log(transformed)
输出:
[
{ name: 'a name', quantity: 32 },
{ name: 'another name', quantity: 50 },
{ name: 'another new name', quantity: 56 }
]