如何在一个对象数组中使用相同的键汇总值?
假设我有类似的对象:
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"
}
}]
每个对象中的键由“类别”键中的值定义。这意味着如果“类别” =“组呈现”,则存在一个称为“ grouppersentation”的密钥,其中使用replace('',','').tolowercase()将“类别”值转换为“类别”值。
它没有如上显示,但是有一个带有“类别” =“数据上下文”的对象,因此在同一对象中有一个称为“ dataContext”的键。
另一个重要说明是,如果“类别” =“组呈现”,则“ grouppersentation”键将具有一个值,即一个数字。其他键将设置为空,因此具有实际值的唯一键是“类别”和“ grouppersentation”。像这样,但使用类别“评估”:
{
"grouppresentation": null,
"evaluation": "46",
"datacontext": null,
"category": "Evaluation"
},
目标
我想要的每个项目都具有相同的“类别”值,以总结代表该值的密钥值:
因此,
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'}
您是否知道如何实现上述方法?
Let's say I have an array of objects like so:
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"
}
}]
The keys in each object is defined by the values in the "category" key. Meaning that if "category" = "Group Presentation", then there exists a key called "grouppresentation" where the "category" value has been converted using replace(' ', '').toLowerCase().
It is not shown above, but there's an object with "category" = "Data Context" and so there's a key in the same object called "datacontext".
Another important note is that if "category" = "Group Presentation", then the "grouppresentation" key will have a value i.e. a number. The other keys will be set to null, so the only keys with an actual value is "category" and "grouppresentation". Like so but with category "Evaluation":
{
"grouppresentation": null,
"evaluation": "46",
"datacontext": null,
"category": "Evaluation"
},
GOAL
I want for each item with the same 'category' value, to sum the values of the key that represents that value:
So,
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"
}
}]
So far, I've attempted several methods, one of which being:
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();
}
});
However, it unfortunately does no get anywhere and yields the following:
{undefined: 'grouppresentation'}
Would you have any idea how achieve the above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
到达那里。基本上,我们只检查每个迭代的类别中的匹配项。如果有匹配,我们将适当的值添加在一起(将两个转换为飞行的数字)
Array#reduce is one way of getting there. Basically we just check each iteration for a match on the category. If there is match, we add the appropriate values together (converting both to numbers on the fly)