Fusionner l'objet JSON avec la même "clé" et ajouter leur "valeur" à l'aide de JavaScript

Mon JSON ressemble à ça

{"rows":[ {"key":["zeit.de"],"value":98}, {"key":["google.com"],"value":49}, {"key":["spiegel.de"],"value":20}, {"key":["spiegel.de"],"value":12}, {"key":["spiegel.de"],"value":20}, {"key":["spiegel.de"],"value":12}, {"key":["netmng.com"],"value":49}, {"key":["zeit.de"],"value":300} ]} 

Et je cherche une solution pour fusionner toutes les mêmes clés et ajouter les valeurs des clés fusionnées ensemble pour obtenir quelque chose comme ça.

 {"rows":[ {"key":["zeit.de"],"value":398}, {"key":["google.com"],"value":49}, {"key":["spiegel.de"],"value":54}, {"key":["netmng.com"],"value":49} ]} 

Merci d'avoir répondu.

 var json = '{"rows":[{"key":["zeit.de"],"value":98},{"key":["google.com"],"value":49},{"key":["spiegel.de"],"value":20},{"key":["spiegel.de"],"value":12},{"key":["spiegel.de"],"value":20},{"key":["spiegel.de"],"value":12},{"key":["netmng.com"],"value":49},{"key":["zeit.de"],"value":300}]}'; var obj = JSON.parse(json); var newObj = {}; for(i in obj['rows']){ var item = obj['rows'][i]; if(newObj[item.key[0]] === undefined){ newObj[item.key[0]] = 0; } newObj[item.key[0]] += item.value; } var result = {}; result.rows = []; for(i in newObj){ result.rows.push({'key':i,'value':newObj[i]}); } console.log(result); 

Voici une solution alternative utilisant les fonctions ECMA-Script Array forEach , some et filter :

 var data = { "rows": [{ "key": ["zeit.de"], "value": 98 }, { "key": ["google.com"], "value": 49 }, { "key": ["spiegel.de"], "value": 20 }, { "key": ["spiegel.de"], "value": 12 }, { "key": ["spiegel.de"], "value": 20 }, { "key": ["spiegel.de"], "value": 12 }, { "key": ["netmng.com"], "value": 49 }, { "key": ["zeit.de"], "value": 300 }] }; var merged = { rows: [] }; data.rows.forEach(function(sourceRow) { debugger; if(!merged.rows.some(function(row) { return row.key[0] == sourceRow.key[0]; })) { merged.rows.push({ key: [sourceRow.key[0]], value: sourceRow.value }); } else { var targetRow = merged.rows.filter(function(targetRow) { return targetRow.key[0] == sourceRow.key[0] })[0]; targetRow.value += sourceRow.value; } }); document.getElementById("result").textContent = JSON.stringify(merged); 
 <div id="result"></div> 

Vous pouvez essayer quelque chose comme ceci:

 var newData = {rows: []}, index; for (var i = 0; i < data.rows.length;i++){ index = newDataContains(data.rows[i].key); if (index == -1){ newData.rows.push({key: data.rows[i].key, value: data.rows[i].value}); } else { newData.rows[index].value += data.rows[i].value; } } function newDataContains(key){ for (var i=0; i < newData.rows.length; i++){ if (JSON.stringify(newData.rows[i].key) == JSON.stringify(key)) { return i; break; } } return -1; } 

Jsfiddle: https://jsfiddle.net/9p81g5j6/

Vous pouvez utiliser lodash

 let result = [{'a': 3, 'b': 2}, {'a': 4, 'c': 1}, {'a': 4, 'c': 1}]; let m = _.reduce((_.uniq(_.flattenDeep(_.map(result, i => _.keys(i))))), function (s, v) { s[v] = _.sumBy(result, function (o) { return o[v]; }); return s }, {}); console.log(m); // { a: 11, b: 2, c: 2 }