1
var  Error-dictionary = [
   {
      code:599,
      MSG:'unknown' 
   },
   {
      code:404, 
      MSG:'not found'
   },
   {
      code:599, 
      MSG:'unknown'
   }
]

I want something like this:

[
  {
     code : 599,
     count:2,  
     MSG : 'unknown', 
     code :404,
     count:1,
     MSG : 'not found' 
  }
] 

and this need to push into $scope.alerts as a MSG

Error code and MSG are dynamic

6
  • what do you do if your MSG is different for the same error code ? Commented Oct 12, 2015 at 14:43
  • 3
    iterate array and create new object. Please show what you tried ... this isn't a code writing service Commented Oct 12, 2015 at 14:47
  • That is also one more case @dlght Commented Oct 12, 2015 at 14:49
  • you can count the number of appearences easily, but how do you deal with the different error codes ? take the most common or the first ? Commented Oct 12, 2015 at 14:55
  • Is the example output supposed to be an array of two objects? That's not a valid javascript object with two copies of the same property. Commented Oct 12, 2015 at 15:01

2 Answers 2

1

You can get the number of an specific property in a array of objects, by using underscore.js. Nice and easy!!!

<script src="http://underscorejs.org/underscore-min.js"></script>
<script>
  window.onload = function () {
    var dict = [
      {code: 599, MSG: 'unknown' },
      {code: 404, MSG: 'not found'},
      {code: 599, MSG: 'unknown'}
    ];

    var res = _.groupBy(dict, function (d) {
      return d.code;
    });

      var dictArr = []; 

        for(var key in res) {

            var code = key; 
            var value = res[key]; 
            var MSG = value[0].MSG; 
            var count = value.length; 
            dictArr.push({ code : code, MSG: MSG, count: count  }); 

        }; 

        console.log(dictArr); 


  };
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

this does not result in what the OP is asking :]
have to know what codes exist and will iterate array for each code ... not very efficient or scaleable
You can't group by msg also. You just need to for-loop this one time to create the object he wants
Now does the result reflect exactly what he does ask. I was not aware he needed so much handholding.
0

Create a temp object that uses the error code and message as keys. Then map that temp object to output array

var tmp = {}, res =[];
data.forEach(function (item) {
    var tmpKey = item.code + item.MSG;
    if (!tmp.hasOwnProperty(tmpKey)) {
        tmp[tmpKey] = angular.copy(item);
        tmp[tmpKey].count = 0;
    }
    tmp[tmpKey].count++;
});

for( var key in tmp){
   res.push(tmp[key]; 
}
$scope.errors = res;
console.log($scope.errors);

1 Comment

angular.copy(item); and $scope.errors = res; is angular?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.