[英]Check if a key value already exists in a multidimensional array object in angularjs
Suppose I have an array with object like this 假设我有一个带有这样对象的数组
array = [
{id:2,cnt:2},{id:3,cnt:3},{id:4,cnt:2},
{id:1,cnt:6},{id:2,cnt:7},{id:5,cnt:4},
{id:2,cnt:4},{id:3,cnt:2},{id:4,cnt:2},
{id:3,cnt:2},{id:4,cnt:3},{id:5,cnt:2}
];
where I need to create another array with the object where I need to add cnt
value with id
. 我需要在其中添加需要添加id
cnt
值的对象创建另一个数组。
Output suppose to be like this. 输出假设是这样的。
output = [
{id:1,cnt:6},{id:2,cnt:13},{id:3,cnt:7},{id:4,cnt:7},{id:5,cnt:6}
];
what I have tried so far is 到目前为止,我尝试过的是
var general = [];
angular.forEach(array, function(value){
angular.forEach(value, function(val,key){
angular.forEach(general, function(val1,key1){
if(val1.id === val.id){
val1.cnt +=val.cnt
//@TOD0 how to add value of count and put it on general
}else{
//@TODO
general.push(val);
}
});
});
});
console.log(general);
I am unable to achieve my output. 我无法实现输出。 I have marked as TODO where I am confused. 在我感到困惑的地方,我将其标记为TODO。 Can someone help me? 有人能帮我吗? Thanks in advance. 提前致谢。
Array.reduce
can help you a lot - you basically create a new array, and iterate your current array and check the new array to see if the current item exists. Array.reduce
可以为您提供很多帮助-您基本上可以创建一个新数组,并迭代当前数组并检查新数组以查看当前项是否存在。 If it does, add the cnt
- else add the whole item: 如果是这样,则添加cnt
否则添加整个项目:
var mashed = arr.reduce(function(m, cur, idx) {
var found = false;
for (var i =0; i < m.length; i++) {
if (m[i].id == cur.id) {
m[i].cnt += cur.cnt;
found = true;
}
}
if (!found) {
m.push(cur)
}
return m;
}, [])
Fiddle demo: https://jsfiddle.net/1ffqv9g0/ 小提琴演示: https : //jsfiddle.net/1ffqv9g0/
var arr = [ {id:2,count:2,color:"red"},{id:3,count:3,color:"black"},{id:4,count:2,color:"white"}, {id:1,count:6,color:"green"},{id:2,count:7,color:"red"},{id:5,count:4,color:"blue"}, {id:2,count:4,color:"red"},{id:3,count:2,color:"black"},{id:4,count:2,color:"white"}, {id:3,count:2,color:"black"},{id:4,count:3,color:"red"},{id:5,count:2,color:"blue"} ]; var obj={}; arr.forEach(function(a){ obj[a.id]=obj[a.id]||[0]; obj[a.id][0]=obj[a.id][0]+a["count"]; obj[a.id][1]=a.color; }) //console.log(obj); var brr=Object.keys(obj).map(function(a){ return {"id":a,"count":obj[a][0],"color":obj[a][1]}; }) console.log(brr);
You could use a hash table for the result. 您可以将哈希表用于结果。 For an ordered result, you could sort the result. 对于有序结果,您可以对结果进行排序。
var array = [{ id: 2, cnt: 2 }, { id: 3, cnt: 3 }, { id: 4, cnt: 2 }, { id: 1, cnt: 6 }, { id: 2, cnt: 7 }, { id: 5, cnt: 4 }, { id: 2, cnt: 4 }, { id: 3, cnt: 2 }, { id: 4, cnt: 2 }, { id: 3, cnt: 2 }, { id: 4, cnt: 3 }, { id: 5, cnt: 2 }], grouped = []; array.forEach(function (a) { if (!this[a.id]) { this[a.id] = { id: a.id, cnt: 0 }; grouped.push(this[a.id]); } this[a.id].cnt += a.cnt; }, Object.create(null)); grouped.sort(function (a, b) { return a.id - b.id; }); console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.