[
[ 'TWENTY', 20 ],
[ 'TWENTY', 20 ],
[ 'TWENTY', 20 ],
[ 'TWENTY', 20 ],
[ 'TEN', 10 ],
[ 'FIVE', 5 ],
[ 'ONE', 1 ],
[ 'QUARTER', 0.25 ],
[ 'QUARTER', 0.25 ],
[ 'DIME', 0.1 ],
[ 'DIME', 0.1 ],
[ 'PENNY', 0.01 ],
[ 'PENNY', 0.01 ],
[ 'PENNY', 0.01 ]
]
我的数组由相同的数组组成。如何合并相同的内容?
答案 0 :(得分:2)
您可以使用reduce
对输入数组中唯一索引0
的总数求和:
const data = [ [ 'TWENTY', 20 ],
[ 'TWENTY', 20 ],
[ 'TWENTY', 20 ],
[ 'TWENTY', 20 ],
[ 'TEN', 10 ],
[ 'FIVE', 5 ],
[ 'ONE', 1 ],
[ 'QUARTER', 0.25 ],
[ 'QUARTER', 0.25 ],
[ 'DIME', 0.1 ],
[ 'DIME', 0.1 ],
[ 'PENNY', 0.01 ],
[ 'PENNY', 0.01 ],
[ 'PENNY', 0.01 ] ]
;
const result = Object.entries(data.reduce((a, e) => {
a[e[0]] = e[1] + (a[e[0]] || 0);
return a;
}, {}));
console.log(result);
答案 1 :(得分:2)
你去了
var a = [
["TWENTY", 20],
["TWENTY", 20],
["TWENTY", 20],
["TWENTY", 20],
["TEN", 10],
["FIVE", 5],
["ONE", 1],
["QUARTER", 0.25],
["QUARTER", 0.25],
["DIME", 0.1],
["DIME", 0.1],
["PENNY", 0.01],
["PENNY", 0.01],
["PENNY", 0.01]
];
var b = a.reduce((acc, curr) => {
if (acc[curr[0]]) acc[curr[0]] += curr[1];
else acc[curr[0]] = curr[1];
return acc;
}, {});
console.log(b);
答案 2 :(得分:2)
数组缩减是您希望用于总计的金额,同时将它们保留为键/值数组,尽管我个人可能将它们映射为{ key: value }
,然后缩减以获取更简单的最终结果。
const group = [
[ 'TWENTY', 20 ],
[ 'TWENTY', 20 ],
[ 'TWENTY', 20 ],
[ 'TWENTY', 20 ],
[ 'TEN', 10 ],
[ 'FIVE', 5 ],
[ 'ONE', 1 ],
[ 'QUARTER', 0.25 ],
[ 'QUARTER', 0.25 ],
[ 'DIME', 0.1 ],
[ 'DIME', 0.1 ],
[ 'PENNY', 0.01 ],
[ 'PENNY', 0.01 ],
[ 'PENNY', 0.01 ]
];
const totals = group.reduce((accumulator, [key, value]) => {
const index = accumulator.findIndex(([k,v]) => k === key);
if (index === -1) {
return [ ...accumulator, [key, value] ];
}
const newTotal = [ key, ( accumulator[index][1] + value ) ];
return [ ...accumulator.slice(0,index), newTotal, ...accumulator.slice(index + 1) ];
},[]);
const totalsAsObject = group.reduce((accumulator, [key, value]) => {
const objectKeys = Object.keys(accumulator);
if (objectKeys.includes(key)) {
return { ...accumulator, [key]: accumulator[key] + value }
}
return { ...accumulator, [key]: value }
},
{}
);
console.log(totals);
console.log(totalsAsObject);
答案 3 :(得分:1)
您可以使用reduce
累加器包装map
函数,并使用Array.from
将结果转换回数组。
result = Array.from(arr.reduce((a, cv) => {
return ([t, amt] = cv,
(a.has(t)) ? a.set(t, a.get(t) + amt) : a.set(t, amt), a)
}, new Map()));
let arr = [
['TWENTY', 20],
['TWENTY', 20],
['TWENTY', 20],
['TWENTY', 20],
['TEN', 10],
['FIVE', 5],
['ONE', 1],
['QUARTER', 0.25],
['QUARTER', 0.25],
['DIME', 0.1],
['DIME', 0.1],
['PENNY', 0.01],
['PENNY', 0.01],
['PENNY', 0.01]
],
result = Array.from(arr.reduce((a, cv) => {
return ([t, amt] = cv,
(a.has(t)) ? a.set(t, a.get(t) + amt) : a.set(t, amt), a)
}, new Map()));
console.log(result);
答案 4 :(得分:0)
ES6替代:
const data = [ [ 'TWENTY', 20 ],
[ 'TWENTY', 20 ],
[ 'TWENTY', 20 ],
[ 'TWENTY', 20 ],
[ 'TEN', 10 ],
[ 'FIVE', 5 ],
[ 'ONE', 1 ],
[ 'QUARTER', 0.25 ],
[ 'QUARTER', 0.25 ],
[ 'DIME', 0.1 ],
[ 'DIME', 0.1 ],
[ 'PENNY', 0.01 ],
[ 'PENNY', 0.01 ],
[ 'PENNY', 0.01 ] ]
const result = data.reduce((o, [k, v]) => (o[k] = v + o[k] || v, o), {})
console.log( result )
console.log( Object.entries(result) )