简体   繁体   English

检查angularjs中的多维数组对象中是否已存在键值

[英]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.

相关问题 AngularJs-检查数组对象中是否存在值 - AngularJs - check if value exists in array object 检查具有日期的键值对已经存在,如果不存在,则将其推入数组 - Check key value pairs with date already exist and if NOT exists push it into an array 检查数组内的对象值是否已存在于其他对象数组中? - Check if object value inside array, already exists in a different array of objects? 检查键值是否存在于数组对象中,如果不存在具有相同键值的数组和短数组内的推对象 - Check key value exists in array object or not, if not exists push object inside array and short array with same key value 如何使用jQuery检查数组中的对象值是否已存在 - How to check if object value in array already exists using jquery 如果键值已经存在,如何从数组中删除对象 - 打字稿 - How to remove object from an array if key value already exists - typescript 如果键在Array.map中已经存在,则将值推送到对象 - Push value to object if key already exists during Array.map 检查对象-&gt;键是否已经在数组中 - Check if Object -> key is already in array 检查数组的每个对象中是否存在键值对 - Check whether key value pair exists in every object of array 检查对象数组中是否存在具有特定键值的对象 - check if an object with a specific key value exists in an array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM