Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 690
Saravanan Avatar asked Aug 23 '13 06:08

Saravanan


People also ask

What is angular extend?

Overview. Extends the destination object dst by copying own enumerable properties from the src object(s) to dst . You can specify multiple src objects. If you want to preserve original objects, you can do so by passing an empty object as the target: var object = angular. extend({}, object1, object2) .

How to iterate list in Angular?

You can loop through an Array or an Object in AngularJS using the forEach() function. The function invokes the iterator function that iterates or loops through each item in an array.

How to use forEach loop in Angular JS?

forEach() Function in AngularJS is used to iterate through each item in an array or object. It works similar to the for loop and this loop contains all properties of an object in key-value pairs of an object. Parameter Values: object: It refers to the object to be iterated.


1 Answers

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) 
like image 73
0xc0de Avatar answered Oct 20 '22 13:10

0xc0de