简体   繁体   中英

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 .

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. 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. If it does, add the cnt - else add the whole item:

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/

 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; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM