gpt4 book ai didi

javascript - 将重复属性的值汇总到一个新对象中

转载 作者:行者123 更新时间:2023-12-01 03:58:53 25 4
gpt4 key购买 nike

我正在尝试总结出现在如下构造的对象数组中的值:

[ 
{ token: 'N97235', conversions: '2', payout: '100' },
{ token: 'N91567', conversions: '2', payout: '100' },
{ token: 'N91567', conversions: '3', payout: '150' },
{ token: 'N97759', conversions: '2', payout: '100' },
{ token: 'N97240', conversions: '1', payout: '50' },
{ token: 'N13925', conversions: '1', payout: '50' },
{ token: 'N53285', conversions: '1', payout: '50' },
{ token: 'N28312', conversions: '1', payout: '100' },
{ token: 'N96475', conversions: '1', payout: '50' },
{ token: 'N97759', conversions: '2', payout: '100' },
{ token: 'N76951', conversions: '1', payout: '100' },
{ token: 'N39108', conversions: '1', payout: '50' },
{ token: 'N39108', conversions: '4', payout: '200' },
{ token: 'N48854', conversions: '2', payout: '100' },
{ token: 'N50179', conversions: '1', payout: '50' },
{ token: 'N18804', conversions: '1', payout: '50' },
{ token: 'N95631', conversions: '2', payout: '100' },
]

我的意思是我想将所有转化和支出汇总到一个如下所示的新对象中

{
N97235: { conversions: 2, payout: 100 },
N39108: { conversions: 5, payout: 250 },
N91567: { conversions: 6, payout: 300},
N96475: { conversions: 1, payout: 50},
// ..and so on
}

正如您所看到的,它在原始数组中找到了重复的“条目”,并将它们的转换和支出汇总到一个新对象中(单个条目也在那里)。我试图做的是遍历这个数组(在本例中为 tokens_array[] )并将其对象属性附加到新创建的对象( conversions_sum{} 一个),但问题是首先将其值“无效”,在向其中添加任何内容之前..

tokens_array.forEach(function(element) {
var token_string = element.token.toString();
conversions_sum[token_string] = {};
conversions_sum[token_string].conversions += element.conversions;
conversions_sum[token_string].payout += element.payout;
});

但由于 conversions_sum[token_string].conversionsconversions_sum[token_string].payout 并未从一开始就初始化 - 它将数字添加为“未定义”。 .

提前致谢。

最佳答案

您可以使用Array.prototype.reduce创建 token 的哈希对象,如下所示:

function group(arr) {
return arr.reduce(function(h, e) { // for each object e in arr
if(h[e.token]) { // if we already hashed the object
h[e.token].conversions += +e.conversions; // add e's conversions to the hashed object (implicitly convert e.conversions into a number using unary +)
h[e.token].payout += +e.payout; // add e's payout to the hashed object as well
}
else { // if not, then create a new object initialized with e's values
h[e.token] = {conversions: +e.conversions, payout: +e.payout};
}

return h;
}, {});
}

var array = [
{ token: 'N97235', conversions: '2', payout: '100' },
{ token: 'N91567', conversions: '2', payout: '100' },
{ token: 'N91567', conversions: '3', payout: '150' },
{ token: 'N97759', conversions: '2', payout: '100' },
{ token: 'N97240', conversions: '1', payout: '50' },
{ token: 'N13925', conversions: '1', payout: '50' },
{ token: 'N53285', conversions: '1', payout: '50' },
{ token: 'N28312', conversions: '1', payout: '100' },
{ token: 'N96475', conversions: '1', payout: '50' },
{ token: 'N97759', conversions: '2', payout: '100' },
{ token: 'N76951', conversions: '1', payout: '100' },
{ token: 'N39108', conversions: '1', payout: '50' },
{ token: 'N39108', conversions: '4', payout: '200' },
{ token: 'N48854', conversions: '2', payout: '100' },
{ token: 'N50179', conversions: '1', payout: '50' },
{ token: 'N18804', conversions: '1', payout: '50' },
{ token: 'N95631', conversions: '2', payout: '100' },
];

console.log(group(array));

关于javascript - 将重复属性的值汇总到一个新对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42356507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com