Angular JS - angular.forEach - How to get key of the object?

angularjs

Solution

The first parameter to the iterator in `forEach` is the value and second is the key of the object.

angular.forEach(objectToIterate, function(value, key) {
    /* do something for all key: value pairs */
});

In your example, the outer forEach is actually:

angular.forEach($scope.filters, function(filterObj , filterKey)

Problem

I have JSON object like below ``` { "txt_inc_Application": { "EWS": true, "EWindow": true }, "txt_inc_IncidentType": { "Brand Damage": true, "Internal failure": true } } ``` And I am using angular.forEach to get the values ``` $scope.filterFormula=function() { angular.forEach($scope.filters, function(filterObj , filterIndex) { angular.forEach(filterObj, function(value , key) { console.log(value+"--"+key) }) }) } ``` How can i get "txt_inc_Application" and "txt_inc_IncidentType" in the loop? Also when call the angular function in html like below why it is getting executed twice? ``` {{filterFormula()}} ```

Original source