for(i=0; i < results.length; i++){
var newObj = {};
newObj.name = results[i].name;
newObj.impact = [results[i].impact, results[i]['count']];
filt.push(newObj);
}
我循环结果并生成了一个对象数组,但是我想组合具有相同键的对象(&#34; name&#34;)。潜在影响值可能不包含相同的密钥,因为可能会错过一个例如user1缺少&#34; 3&#34;级别,但user2拥有它。如何组合对象数组来折叠它们?
{ name: 'User1', impact: [ '1', 1 ] }
{ name: 'User1', impact: [ '2', 1 ] }
{ name: 'User1', impact: [ '4', 2 ] }
{ name: 'User1 ', impact: [ '0', 1 ] }
{ name: 'User2 ', impact: [ '1', 1 ] }
{ name: 'User2 ', impact: [ '2', 1 ] }
{ name: 'User2 ', impact: [ '3', 1 ] }
{ name: 'User2 ', impact: [ '4', 1 ] }
希望它是:
{ name: 'User1', impact: ['0', 1, '1', 1, '2', 1, '3', 0, '4', 2] }
{ name: 'User2', impact: ['0', 1, '1', 1, '2', 1, '3', 1, '4', 1] }