问题描述
伙计们,我需要一点帮助才能解决此问题.
Hi Guys i need small help in getting this solved.
如果这是重复的帖子,请指出原始问题.
If this is a duplicate post please point me to the original question.
这里有一个JSON元素数组
Here I have an JSON array of elements
Ex:var consume = [{"key":"Test1"},{"key":"Test2"},{"key":"Test3"},{"key":"Test1"},{"key":"Test3"},{"key":"Test1"}];
预期输出:
var yield = [{"name":"test1","count":3},{"name":"test2","count":2},{"name":"test3","count":2}]
var produce = [{"name":"test1","count":3},{"name":"test2","count":2},{"name":"test3","count":2}]
推荐答案
最后,我用纯JavaScript编写了我的问题的答案.
Finally I wrote Answer to my question in pure javascript.
感谢您的支持人员,他们曾经试图引导我解决我的问题
Thanks for your support guys who ever tried to guide me to solve my question
var consume = [{"key":"Test1"},{"key":"Test2"},{"key":"Test3"},{"key":"Test1"},{"key":"Test3"},{"key":"Test1"}]
var temp = [];
var produce = [];
for(var i=0;i<consume.length;i++){
if(temp.indexOf(consume[i].key) == -1){
temp.push(consume[i].key);
var _data = {};
_data.name = consume[i].key;
_data.count = 1;
produce.push(_data);
}else{
for(var j=0;j<produce.length;j++){
if(produce[j].name === consume[i].key){
var _x = parseInt(produce[j].count) + 1;
produce[j].count = _x;
}
}
}
}
console.log(produce);
这篇关于如何获取分配给JSON数组中相同键的重复值的计数-JavaScript/NodeJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!