22

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()}}

enter image description here

6
  • try $scope.filterFormula=function() { angular.forEach($scope.filters, function(filterObj , filterIndex) { angular.forEach(filterObj, function(value , key) { console.log(filterIndex) }) }) } Commented Aug 23, 2013 at 6:35
  • 1
    Bindings are called at least once per apply-digest cycle. AngularJS keeps checking all bindings until nothing has changed in a cycle. So if it checks filterFormula, and something in the model changes in the same cycle, it will be called again! And again! And again! Up to 10 times until you get the error 10 $digest iterations reached. Commented Aug 23, 2013 at 6:46
  • Thanks. Silly me. I dont know why I ignored that. Also any idea why filterFormula function is getting executed twice? I can see console .log is printed twice. Commented Aug 23, 2013 at 6:47
  • Yes. added image in main post Commented Aug 23, 2013 at 6:55
  • My comment describes why you get this behaviour! AngularJS must call it again to see if the model has changed. Commented Aug 23, 2013 at 6:59

2 Answers 2

48

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)
Sign up to request clarification or add additional context in comments.

Comments

2
var obj = {name: 'Krishna', gender: 'male'};
angular.forEach(obj, function(value, key) {
    console.log(key + ': ' + value);
});

yields the attributes of obj with their respective values:

name: Krishna
gender: male

2 Comments

Welcome to Stack Overflow! Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how and why this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.
Yields is a misleading verb here, your code is not yielding anything here, just logging to console.

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.