javascript - How to calculate count for duplicated Item along with there status -


_datadetails = {     name : test1 ,     status : found,     other : [] } {     name : test1 ,     status : found,     other : [] } {     name : test1 ,     status : not found,     other : [] } {     name : test2 ,     status : found,     other : [] }etc ........ 

i want calculate count found , not found each names.

for example: test1  found:2 notfound :1  test2  found:1 notfound :0 

may code

var _data = [] var index = -1; ( var _i = 0; _i < _data.length; _i++ ) {     if ( _data[_i][0] == _datadetails[i].name ) { index = _i; } }   if ( index >= 0 ) {     if ( _datadetails[i].status == 'found' )     {         _data[index][1][0]++;     }     else     {         _data[index][1][1]++;     } } else {     if ( _datadetails[i].result == 'detected' )     {         _data.push([_datadetails[i].name, [1, 0]]);     }     else     {         _data.push([_datadetails[i].name, [0, 1]]);     } } 

here getting count doubled (instead of 10 give 20). please suggest .

you can this.

    _datadetails = [{         name : "test1" ,         status : "found",         other : []     },     {         name : "test1" ,         status : "found",         other : []     },     {         name : "test1" ,         status : "not found" ,         other : []     },     {         name :"test2" ,         status : "found",         other : []     }]      var results={};     _datadetails.foreach ( function (o,i) {         if(!results[o.name]) {      results[o.name] = {} ;     results[o.name].found = 0;     results[o.name].notfound = 0;     }         if (o.status === "found" ) {     results[o.name].found++}        else if (o.status === "not found" ) {results[o.name].notfound++}     });      object.getownpropertynames(results).foreach(function(prop) {     console.log( prop + "  found : " + results[prop].found + " ,  not found: "+ results[prop].notfound);     }); 

see here: http://jsbin.com/kavesu/edit?js,console


Comments