如何根据对象数组中的相似值(而非属性)对对象数组执行 sum/minus 操作
how to perform sum/minus operation on an array of objects, based on similar values(not properties) in an array of objects
我有一个这样的对象数组:
i/p:
let payments=[
{from:"b",to:"c",amount:30}, {from:"a",to:"c",amount:30},
{from:"c",to:"a",amount:50}, {from:"b",to:"a",amount:50},
{from:"c",to:"b",amount:66.66}, {from:"a",to:"b",amount:66.66},
{from:"a",to:"c",amount:150}, {from:"b",to:"c",amount:150},
{from:"a", to:"c",amount:75}, {from:"b", to:"c",amount:125}
]
现在我想根据相同的 属性 值对金额 属性 执行求和运算,并根据反向匹配 属性 值对金额 属性 执行减法运算(i,e from,to),将数组缩减为:
o/p:
[
{ from: "b", to: "c", amount: 238.34 }, //(sum of all b->c amounts) minus (sum of all c->b amounts)
{ from: "a", to: "c", amount: 205 }, //(sum of all a->c amounts) minus (sum of all c-a amounts)
{ from: "a", to: "b", amount: 16.66 }, //(sum of all a->b amounts) minus (sum of all b->a amounts)
];
注意:结果数组数量 属性 不应为负数
我已经尝试了一些方法,但无法实现。任何帮助,将不胜感激。谢谢
也许这会有所帮助:
let payments=[
{from:"b",to:"c",amount:30}, {from:"a",to:"c",amount:30},
{from:"c",to:"a",amount:50}, {from:"b",to:"a",amount:50},
{from:"c",to:"b",amount:66.66}, {from:"a",to:"b",amount:66.66},
{from:"a",to:"c",amount:150}, {from:"b",to:"c",amount:150},
{from:"a", to:"c",amount:75}, {from:"b", to:"c",amount:125}
];
let pay = Object.entries(
payments.reduce((tot,{from,to,amount})=>{
let u=from+'-'+to; // from-to-relationship (in alphabetical order)
if (to<from) { u=to+'-'+from; amount=-amount }
tot[u]=(tot[u]||0)+amount; // collate all payments here
return tot;
}, {})
).map(([u,a])=>{let ft=u.split('-');
if (a<0) {ft=ft.reverse();a=-a}
return {from:ft[0], to:ft[1], amount:a.toFixed(2)}
})
console.log(pay)
在 .reduce
函数中,我将两个合作伙伴之间的所有付款整理到一个帐户对象中,其中“acounts”以“from-to”命名,此名称始终按字母顺序建立,因此,如果我遇到付款{from:"b", to:"a", amount:123}
我调单让金额为负:
if (to<from) { u=to+'-'+from; amount=-amount }
完成此操作后,我使用 Object.entries
遍历帐户对象。这将允许我再次将帐户名称分为 from
和 to
合作伙伴。这次我检查合作伙伴之间的应付金额是否为负数。如果是这样,我交换合作伙伴的顺序 (ft
) 并将 amonut (a
) 乘以 -1:
if (a<0) {ft=ft.reverse();a=-a}
我有一个这样的对象数组:
i/p:
let payments=[
{from:"b",to:"c",amount:30}, {from:"a",to:"c",amount:30},
{from:"c",to:"a",amount:50}, {from:"b",to:"a",amount:50},
{from:"c",to:"b",amount:66.66}, {from:"a",to:"b",amount:66.66},
{from:"a",to:"c",amount:150}, {from:"b",to:"c",amount:150},
{from:"a", to:"c",amount:75}, {from:"b", to:"c",amount:125}
]
现在我想根据相同的 属性 值对金额 属性 执行求和运算,并根据反向匹配 属性 值对金额 属性 执行减法运算(i,e from,to),将数组缩减为:
o/p:
[
{ from: "b", to: "c", amount: 238.34 }, //(sum of all b->c amounts) minus (sum of all c->b amounts)
{ from: "a", to: "c", amount: 205 }, //(sum of all a->c amounts) minus (sum of all c-a amounts)
{ from: "a", to: "b", amount: 16.66 }, //(sum of all a->b amounts) minus (sum of all b->a amounts)
];
注意:结果数组数量 属性 不应为负数
我已经尝试了一些方法,但无法实现。任何帮助,将不胜感激。谢谢
也许这会有所帮助:
let payments=[
{from:"b",to:"c",amount:30}, {from:"a",to:"c",amount:30},
{from:"c",to:"a",amount:50}, {from:"b",to:"a",amount:50},
{from:"c",to:"b",amount:66.66}, {from:"a",to:"b",amount:66.66},
{from:"a",to:"c",amount:150}, {from:"b",to:"c",amount:150},
{from:"a", to:"c",amount:75}, {from:"b", to:"c",amount:125}
];
let pay = Object.entries(
payments.reduce((tot,{from,to,amount})=>{
let u=from+'-'+to; // from-to-relationship (in alphabetical order)
if (to<from) { u=to+'-'+from; amount=-amount }
tot[u]=(tot[u]||0)+amount; // collate all payments here
return tot;
}, {})
).map(([u,a])=>{let ft=u.split('-');
if (a<0) {ft=ft.reverse();a=-a}
return {from:ft[0], to:ft[1], amount:a.toFixed(2)}
})
console.log(pay)
在 .reduce
函数中,我将两个合作伙伴之间的所有付款整理到一个帐户对象中,其中“acounts”以“from-to”命名,此名称始终按字母顺序建立,因此,如果我遇到付款{from:"b", to:"a", amount:123}
我调单让金额为负:
if (to<from) { u=to+'-'+from; amount=-amount }
完成此操作后,我使用 Object.entries
遍历帐户对象。这将允许我再次将帐户名称分为 from
和 to
合作伙伴。这次我检查合作伙伴之间的应付金额是否为负数。如果是这样,我交换合作伙伴的顺序 (ft
) 并将 amonut (a
) 乘以 -1:
if (a<0) {ft=ft.reverse();a=-a}