如何在对象数组中使用相同的键对值求和?
How to sum values with the same key In an array of objects?
假设我有一个像这样的对象数组:
this.arrayOfObjects = [{
{
"grouppresentation": "11",
"evaluation": null,
"datacontext": null,
"category": "Group Presentation"
},
{
"grouppresentation": null,
"evaluation": "23",
"datacontext": null,
"category": "Evaluation"
},
{
"grouppresentation": null,
"evaluation": "46",
"datacontext": null,
"category": "Evaluation"
},
{
"grouppresentation": "44",
"evaluation": null,
"datacontext": null,
"category": "Group Presentation"
},
{
"grouppresentation": null,
"evaluation": null,
"datacontext": "21",
"category": "Data Context"
}
}]
每个对象中的键由“类别”键中的值定义。这意味着如果“category”=“Group Presentation”,则存在一个名为“grouppresentation”的键,其中“category”值已使用 replace(' ', '').toLowerCase().
上面没有显示,但是有一个对象带有“category”=“Data Context”,所以在同一个对象中有一个名为“datacontext”的键。
另一个重要的注意事项是,如果“category”=“Group Presentation”,那么“grouppresentation”键将有一个值,即一个数字。其他键将设置为空,因此具有实际值的唯一键是“category”和“grouppresentation”。像这样,但类别为“评估”:
{
"grouppresentation": null,
"evaluation": "46",
"datacontext": null,
"category": "Evaluation"
},
目标
我想对具有相同 'category' 值的每个项目求和代表该值的键的值:
所以,
this.newArrayOfObjects = [{
{
"grouppresentation": "55", -> because 11 + 44 from the values above
"evaluation": null,
"datacontext": null,
"category": "Group Presentation"
},
{
"grouppresentation": null,
"evaluation": "69", -> because 46 + 23 from the values above
"datacontext": null,
"category": "Evaluation"
},
{
"grouppresentation": null,
"evaluation": null,
"datacontext": "21",
"category": "Data Context"
}
}]
到目前为止,我尝试了几种方法,其中之一是:
var newObject = {};
this.arrayOfObjects.forEach(function(item) {
if (newObject.hasOwnProperty(item.category)) {
newObject[item.name] = newObject[item.category.replace(' ', '').toLowerCase()] + item.category.replace(' ', '').toLowerCase();
} else {
newObject[item.name] = item.category.replace(' ', '').toLowerCase();
}
});
然而,不幸的是,它没有得到任何结果并产生以下结果:
{undefined: 'grouppresentation'}
您知道如何实现上述目标吗?
Array#reduce 是到达那里的一种方式。基本上,我们只是检查每个迭代是否与类别匹配。如果匹配,我们将适当的值加在一起(即时将两者转换为数字)
let arrayOfObjects = [{
"grouppresentation": "11",
"evaluation": null,
"datacontext": null,
"category": "Group Presentation"
},
{
"grouppresentation": null,
"evaluation": "23",
"datacontext": null,
"category": "Evaluation"
},
{
"grouppresentation": null,
"evaluation": "46",
"datacontext": null,
"category": "Evaluation"
},
{
"grouppresentation": "44",
"evaluation": null,
"datacontext": null,
"category": "Group Presentation"
},
{
"grouppresentation": null,
"evaluation": null,
"datacontext": "21",
"category": "Data Context"
}
]
let newArrayOfObjects = arrayOfObjects.reduce((b, a) => {
let ind = b.findIndex(e => e.category === a.category);
let c = a.category.toLowerCase().split(' ').join('');
if (ind > -1) {
b[ind][c] = +b[ind][c] + +a[c]
} else {
a[c] = +a[c] || 0
b.push(a)
}
return b;
}, []);
console.log(newArrayOfObjects)
假设我有一个像这样的对象数组:
this.arrayOfObjects = [{
{
"grouppresentation": "11",
"evaluation": null,
"datacontext": null,
"category": "Group Presentation"
},
{
"grouppresentation": null,
"evaluation": "23",
"datacontext": null,
"category": "Evaluation"
},
{
"grouppresentation": null,
"evaluation": "46",
"datacontext": null,
"category": "Evaluation"
},
{
"grouppresentation": "44",
"evaluation": null,
"datacontext": null,
"category": "Group Presentation"
},
{
"grouppresentation": null,
"evaluation": null,
"datacontext": "21",
"category": "Data Context"
}
}]
每个对象中的键由“类别”键中的值定义。这意味着如果“category”=“Group Presentation”,则存在一个名为“grouppresentation”的键,其中“category”值已使用 replace(' ', '').toLowerCase().
上面没有显示,但是有一个对象带有“category”=“Data Context”,所以在同一个对象中有一个名为“datacontext”的键。
另一个重要的注意事项是,如果“category”=“Group Presentation”,那么“grouppresentation”键将有一个值,即一个数字。其他键将设置为空,因此具有实际值的唯一键是“category”和“grouppresentation”。像这样,但类别为“评估”:
{
"grouppresentation": null,
"evaluation": "46",
"datacontext": null,
"category": "Evaluation"
},
目标
我想对具有相同 'category' 值的每个项目求和代表该值的键的值:
所以,
this.newArrayOfObjects = [{
{
"grouppresentation": "55", -> because 11 + 44 from the values above
"evaluation": null,
"datacontext": null,
"category": "Group Presentation"
},
{
"grouppresentation": null,
"evaluation": "69", -> because 46 + 23 from the values above
"datacontext": null,
"category": "Evaluation"
},
{
"grouppresentation": null,
"evaluation": null,
"datacontext": "21",
"category": "Data Context"
}
}]
到目前为止,我尝试了几种方法,其中之一是:
var newObject = {};
this.arrayOfObjects.forEach(function(item) {
if (newObject.hasOwnProperty(item.category)) {
newObject[item.name] = newObject[item.category.replace(' ', '').toLowerCase()] + item.category.replace(' ', '').toLowerCase();
} else {
newObject[item.name] = item.category.replace(' ', '').toLowerCase();
}
});
然而,不幸的是,它没有得到任何结果并产生以下结果:
{undefined: 'grouppresentation'}
您知道如何实现上述目标吗?
Array#reduce 是到达那里的一种方式。基本上,我们只是检查每个迭代是否与类别匹配。如果匹配,我们将适当的值加在一起(即时将两者转换为数字)
let arrayOfObjects = [{
"grouppresentation": "11",
"evaluation": null,
"datacontext": null,
"category": "Group Presentation"
},
{
"grouppresentation": null,
"evaluation": "23",
"datacontext": null,
"category": "Evaluation"
},
{
"grouppresentation": null,
"evaluation": "46",
"datacontext": null,
"category": "Evaluation"
},
{
"grouppresentation": "44",
"evaluation": null,
"datacontext": null,
"category": "Group Presentation"
},
{
"grouppresentation": null,
"evaluation": null,
"datacontext": "21",
"category": "Data Context"
}
]
let newArrayOfObjects = arrayOfObjects.reduce((b, a) => {
let ind = b.findIndex(e => e.category === a.category);
let c = a.category.toLowerCase().split(' ').join('');
if (ind > -1) {
b[ind][c] = +b[ind][c] + +a[c]
} else {
a[c] = +a[c] || 0
b.push(a)
}
return b;
}, []);
console.log(newArrayOfObjects)