如何在数组上使用 javascript reduce 方法,该数组是对象的 属性 的值,并仅输出正元素的总和?
How to use javascript reduce method on an array which is a value for a property of an object and output a sum of only for positive elements?
我有四个对象(account1
、account2
、account3
、account4
)如下,我想为它们获得 'sum of all deposits' 和 'sum of all withdrawals'。此处的存款和取款以变动 属性 显示。这里所有的正值都是存款,负值是取款。但我只想使用没有 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]
但是,如何设置 movements 数组,以便我可以获得正值之和(即存款)和负值之和(即取款)。
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];
您可以 运行 使用过滤器减少两个总和,或者一次合并减少 positive/negative 个总和:
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);
可能是这个?
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 }
你可以用array#reduce
来总结deposits
和withdrawals
。使用 array#reduce
迭代每个 account
,然后使用 array#forEach
迭代每个 movements
,在结果累加器中求和 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 }
你可以像这样使用.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})
我有四个对象(account1
、account2
、account3
、account4
)如下,我想为它们获得 'sum of all deposits' 和 'sum of all withdrawals'。此处的存款和取款以变动 属性 显示。这里所有的正值都是存款,负值是取款。但我只想使用没有 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]
但是,如何设置 movements 数组,以便我可以获得正值之和(即存款)和负值之和(即取款)。
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];
您可以 运行 使用过滤器减少两个总和,或者一次合并减少 positive/negative 个总和:
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);
可能是这个?
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 }
你可以用array#reduce
来总结deposits
和withdrawals
。使用 array#reduce
迭代每个 account
,然后使用 array#forEach
迭代每个 movements
,在结果累加器中求和 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 }
你可以像这样使用.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})