总结重复的属性'将值转换为新对象

时间:2017-02-21 00:18:46

标签: javascript arrays

我试图总结出现在这样构造的对象数组中的值:

[ 
  { 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] .conversions conversions_sum [token_string] .payout 没有从第一个位置初始化 - 它将数字添加到&# 39;未定义' ..

提前致谢。

2 个答案:

答案 0 :(得分:2)

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

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));

答案 1 :(得分:0)

那么,初始化它们吧?您每次都会覆盖您的新对象,但您只想在它不存在的时候。像所以:

 conversions_sum[token_string] = conversions_sum[token_string] || {
   conversions: 0,
   payout: 0
 };
conversions_sum[token_string].conversions += parseInt(element.conversions, 10);
conversions_sum[token_string].payout += parseInt(element.payout, 10);