我想合并多个在同一个数组中且只有一个对象的对象,我需要在不同的对象中添加具有相同键的元素的值。这是我将使用的数组和对象的一个示例。我不知道钥匙是什么,这使一切变得复杂。我正在使用nodejs。
var array = [{
RT: 0.3396966806905289,
Real: 0.03721461991442617,
fired: 0.26138293747172237,
'Real Madrid': 0.15767570563383954,
Madrid: 0.2771992640023346,
Zidane: 0.3218878635800967,
'Florentino Perez': 0.31535141126767907,
leave: 0.1632670522868711,
manager: 0.12690962022792524,
tancredipalmeri: 0.3218878635800967,
Perez: 0.4543985280046692,
Florentino: 0.44189852800466917
},
{
Cannavaro: 0.2709492640023346,
Ashley: 0.2771992640023346,
'Ashley Cole': 0.15767570563383954,
Casillas: 0.3896966806905289,
Ronaldo: 0.22094926400233458,
Zidane: 0.22094926400233458,
Messi: 0.22094926400233458,
Lampard: 0.22094926400233458,
Iniesta: 0.22094926400233458,
Terry: 0.2771992640023346,
Henry: 0.19890866571173632,
Cole: 0.22094926400233458,
Ivanovic: 0.3218878635800967
},
{
RT: 0.3396966806905289,
effleurer: 0.22094926400233458,
'n\'a': 0.2709492640023346,
top: 0.13189246756926315,
Zidane: 0.2709492640023346,
entraineur: 0.22094926400233458,
monde: 0.22094926400233458,
au: 0.2271992640023346,
'au monde': 0.20434237230050623,
Mvssi10: 0.3218878635800967,
JAMAIS: 0.2709492640023346
},
{
RT: 0.3396966806905289,
Real: 0.10790900513119943,
FrenchRMCF: 0.3218878635800967,
en: 0.2271992640023346,
histoire: 0.2709492640023346,
coach: 0.2271992640023346,
au: 0.2271992640023346,
Zidane: 0.22094926400233458,
'en tant': 0.15767570563383954,
tant: 0.22094926400233458
}
]
答案 0 :(得分:2)
您可以使用reduce来迭代每个对象的对象键:
const res = array.reduce((a, b) => {
Object.keys(b).forEach(k => {
a[k] = (a[k] || 0) + b[k];
});
return a;
}, Object.create(null));
console.log(res);
<script>
const array = [
{
RT: 0.3396966806905289,
Real: 0.03721461991442617,
fired: 0.26138293747172237,
'Real Madrid': 0.15767570563383954,
Madrid: 0.2771992640023346,
Zidane: 0.3218878635800967,
'Florentino Perez': 0.31535141126767907,
leave: 0.1632670522868711,
manager: 0.12690962022792524,
tancredipalmeri: 0.3218878635800967,
Perez: 0.4543985280046692,
Florentino: 0.44189852800466917
},
{
Cannavaro: 0.2709492640023346,
Ashley: 0.2771992640023346,
'Ashley Cole': 0.15767570563383954,
Casillas: 0.3896966806905289,
Ronaldo: 0.22094926400233458,
Zidane: 0.22094926400233458,
Messi: 0.22094926400233458,
Lampard: 0.22094926400233458,
Iniesta: 0.22094926400233458,
Terry: 0.2771992640023346,
Henry: 0.19890866571173632,
Cole: 0.22094926400233458,
Ivanovic: 0.3218878635800967
},
{
RT: 0.3396966806905289,
effleurer: 0.22094926400233458,
'n\'a': 0.2709492640023346,
top: 0.13189246756926315,
Zidane: 0.2709492640023346,
entraineur: 0.22094926400233458,
monde: 0.22094926400233458,
au: 0.2271992640023346,
'au monde': 0.20434237230050623,
Mvssi10: 0.3218878635800967,
JAMAIS: 0.2709492640023346
},
{
RT: 0.3396966806905289,
Real: 0.10790900513119943,
FrenchRMCF: 0.3218878635800967,
en: 0.2271992640023346,
histoire: 0.2709492640023346,
coach: 0.2271992640023346,
au: 0.2271992640023346,
Zidane: 0.22094926400233458,
'en tant': 0.15767570563383954,
tant: 0.22094926400233458
}
]
</script>