我有四个对象(account1
、account2
、account3
、account4
)如下,我想获取“所有存款的总和”和“总和”所有提款的”。这里存款和取款显示在移动属性中。这里所有的正值都是存款,负值是取款。但我只想使用没有 flatMap
和 filter
方法的 reduce 方法。我通过执行以下操作获得了所有动作:
const transactions = accounts.reduce(
(acc, cur) => acc.concat(cur.movements),
[]
);
这将减少到 [200, 450, -400, 3000, -650, -130, 70, 1300, 5000, 3400, -150, -790, -3210, -1000, 8500, -30, 200, -200, 340, -300, -20, 50, 400, -460, 430, 1000, 700, 50, 90]
但是,如何设置运动数组,以便我可以获得正值(存款)和负值(取款)的总和。
const account1 = {
owner: 'Jonas Schmedtmann',
movements: [200, 450, -400, 3000, -650, -130, 70, 1300],
interestRate: 1.2, // %
pin: 1111,
};
const account2 = {
owner: 'Jessica Davis',
movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],
interestRate: 1.5,
pin: 2222,
};
const account3 = {
owner: 'Steven Thomas Williams',
movements: [200, -200, 340, -300, -20, 50, 400, -460],
interestRate: 0.7,
pin: 3333,
};
const account4 = {
owner: 'Sarah Smith',
movements: [430, 1000, 700, 50, 90],
interestRate: 1,
pin: 4444,
};
const accounts = [account1, account2, account3, account4];
答案 0 :(得分:0)
您可以使用过滤器运行两次总和减少,或者同时跟踪正/负总和的组合减少:
const amounts = [200, 450, -400, 3000, -650, -130, 70, 1300, 5000, 3400, -150, -790, -3210, -1000, 8500, -30, 200, -200, 340, -300, -20, 50, 400, -460, 430, 1000, 700, 50, 90];
const pos = amounts.filter(am => am > 0).reduce((tot, v) => tot + v);
const neg = amounts.filter(am => am < 0).reduce((tot, v) => tot + v);
console.log('pos:', pos);
console.log('neg:', neg);
// or alternatively
const combo = amounts.reduce(([pos, neg], val) => val > 0 ? [pos + val, neg] : [pos, neg + val], [0, 0]);
console.log('combo:', combo);
答案 1 :(得分:0)
可能是这个吗?
const
account1 = { owner: 'Jonas Schmedtmann',movements: [200, 450, -400, 3000, -650, -130, 70, 1300],interestRate: 1.2, pin: 1111 }
, account2 = { owner: 'Jessica Davis',movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],interestRate: 1.5, pin: 2222 }
, account3 = { owner: 'Steven Thomas Williams',movements: [200, -200, 340, -300, -20, 50, 400, -460],interestRate: 0.7, pin: 3333 }
, account4 = { owner: 'Sarah Smith',movements: [430, 1000, 700, 50, 90],interestRate: 1,pin: 4444 }
;
const transactions = (...accounts) =>
accounts.reduce((r,{movements})=>
{
movements.forEach(v =>{ v > 0 ? r.deposits += v : r.withdrawals += v })
return r
}, {deposits:0,withdrawals:0})
console.log( transactions( account1, account2, account3, account4 ) )
.as-console-wrapper { max-height: 100% !important; top: 0 }
答案 2 :(得分:0)
您可以使用 array#reduce
来总结 deposits
和 withdrawals
。使用 account
迭代每个 array#reduce
,然后使用 movements
迭代每个 array#forEach
,在结果累加器中总结 deposits
和 withdrawals
。
const result = accounts.reduce((r, {movements}) => {
movements.forEach(v => {
v > 0 ? r.deposits += v : r.withdrawals += v;
});
return r;
},{deposits: 0, withdrawals: 0});
const account1 = {
owner: 'Jonas Schmedtmann',
movements: [200, 450, -400, 3000, -650, -130, 70, 1300],
interestRate: 1.2, // %
pin: 1111,
};
const account2 = {
owner: 'Jessica Davis',
movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],
interestRate: 1.5,
pin: 2222,
};
const account3 = {
owner: 'Steven Thomas Williams',
movements: [200, -200, 340, -300, -20, 50, 400, -460],
interestRate: 0.7,
pin: 3333,
};
const account4 = {
owner: 'Sarah Smith',
movements: [430, 1000, 700, 50, 90],
interestRate: 1,
pin: 4444,
};
const accounts = [account1, account2, account3, account4];
const result = accounts.reduce((r, {movements}) => {
movements.forEach(v => {
v > 0 ? r.deposits += v : r.withdrawals += v;
});
return r;
},{deposits: 0, withdrawals: 0});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0 }
答案 3 :(得分:0)
您可以像这样使用 .reduce
来迭代和累积正负运动。
movements.reduce((acc, movement) => {
if (movement > 0) acc[0] += movement
else acc[1] += movement
return acc
}, [0, 0])
这看起来像一个函数。
let allTransactions = [200, 450, -400, 3000, -650, -130, 70, 1300, 5000, 3400, -150, -790, -3210, -1000, 8500, -30, 200, -200, 340, -300, -20, 50, 400, -460, 430, 1000, 700, 50, 90]
function getPosNeg(movements) {
return movements.reduce((acc, movement) => {
if (movement > 0) acc[0] += movement
else acc[1] += movement
return acc
}, [0, 0])
}
let [positive, negative] = getPosNeg(allTransactions)
console.log({positive: positive, negative: negative})