用节点分组 / Javascript
Group By with Node / Javascript
我有一个 JS 数组,其中还包含数组,每个数组都包含一堆结构如下所示的对象。
const arr1 = [
[{symbol: "xy", balance: 2155113}, {symbol: "asda", balance: 21231231}],
[{symbol: "asda", balance: 6543}, {symbol: "xy", balance: 21678}]
]
我需要的是一个输出数组,它按“符号”属性 对所有子数组进行分组,并将余额相加 (+)。
最后我想要一个这样的数组
[
{symbol: xy, summedBalance: 213131313},
{symbol: yz, summedBalance: 6788767867},
]
普通 javascript 或像 lodash 这样的模块是否可行?
尝试这样的事情:
const arr1 = [
[{symbol: "xy", balance: 2155113}, {symbol: "asda", balance: 21231231}],
[{symbol: "asda", balance: 6543}, {symbol: "xy", balance: 21678}]
]
// Some variables
const knownSymbolsArr = [];
const finalArr = [];
// First degroup your array
const degroupedArr = arr1.flat();
// Iterate and sum balances
degroupedArr.forEach(el => {
// Unknown symbol, process
if (knownSymbolsArr.indexOf(el.symbol) === -1) {
// Sum balances when symbol is el.symbol
const balance = degroupedArr.reduce((acc, val) => val.symbol === el.symbol ? acc + val.balance : acc, 0);
// Add symbol in our knownSymbolsArr
knownSymbolsArr.push(el.symbol);
// Add data in our final array
finalArr.push({
symbol: el.symbol,
balance: balance
});
}
});
console.log(finalArr);
解决方案可以分为两部分:
- 展平输入数组
- 将展平数组的元素按
symbol
分组并计算总和。
const ar = [
[{symbol: "xy", balance: 2155113}, {symbol: "asda", balance: 21231231}],
[{symbol: "asda", balance: 6543}, {symbol: "xy", balance: 21678}]
]
const flattenedAr = ar.reduce((acc, curElement) => {
acc = [...acc, ...curElement];
return acc;
}, []);
const groupedAr = flattenedAr.reduce((group, curElement) => {
const matchingElement = group.find(ele => ele.symbol === curElement.symbol);
if(matchingElement) {
matchingElement.summedBalance += curElement.balance;
} else {
group.push({
symbol: curElement.symbol,
summedBalance: curElement.balance
});
}
return group
}, [])
console.log(groupedAr)
我有一个 JS 数组,其中还包含数组,每个数组都包含一堆结构如下所示的对象。
const arr1 = [
[{symbol: "xy", balance: 2155113}, {symbol: "asda", balance: 21231231}],
[{symbol: "asda", balance: 6543}, {symbol: "xy", balance: 21678}]
]
我需要的是一个输出数组,它按“符号”属性 对所有子数组进行分组,并将余额相加 (+)。
最后我想要一个这样的数组
[
{symbol: xy, summedBalance: 213131313},
{symbol: yz, summedBalance: 6788767867},
]
普通 javascript 或像 lodash 这样的模块是否可行?
尝试这样的事情:
const arr1 = [
[{symbol: "xy", balance: 2155113}, {symbol: "asda", balance: 21231231}],
[{symbol: "asda", balance: 6543}, {symbol: "xy", balance: 21678}]
]
// Some variables
const knownSymbolsArr = [];
const finalArr = [];
// First degroup your array
const degroupedArr = arr1.flat();
// Iterate and sum balances
degroupedArr.forEach(el => {
// Unknown symbol, process
if (knownSymbolsArr.indexOf(el.symbol) === -1) {
// Sum balances when symbol is el.symbol
const balance = degroupedArr.reduce((acc, val) => val.symbol === el.symbol ? acc + val.balance : acc, 0);
// Add symbol in our knownSymbolsArr
knownSymbolsArr.push(el.symbol);
// Add data in our final array
finalArr.push({
symbol: el.symbol,
balance: balance
});
}
});
console.log(finalArr);
解决方案可以分为两部分:
- 展平输入数组
- 将展平数组的元素按
symbol
分组并计算总和。
const ar = [
[{symbol: "xy", balance: 2155113}, {symbol: "asda", balance: 21231231}],
[{symbol: "asda", balance: 6543}, {symbol: "xy", balance: 21678}]
]
const flattenedAr = ar.reduce((acc, curElement) => {
acc = [...acc, ...curElement];
return acc;
}, []);
const groupedAr = flattenedAr.reduce((group, curElement) => {
const matchingElement = group.find(ele => ele.symbol === curElement.symbol);
if(matchingElement) {
matchingElement.summedBalance += curElement.balance;
} else {
group.push({
symbol: curElement.symbol,
summedBalance: curElement.balance
});
}
return group
}, [])
console.log(groupedAr)