How does angularjs.forEach () work?

I have an object:

var obj = {
     a : 5,
     b : 6,
     c : 7
}

I want to set zero for all properties of an object using angular.forEach():

angular.forEach(obj, function(value, key) {
    value = 0;
});

I displayed their values ​​with console.log(), and I found that none of them are equal to zero .

but when i do

obj.a = 0;
obj.b = 0;
obj.c = 0;

Their values ​​are zero.

Can someone explain about this?

+4
source share
1 answer

You simply assign a valuevalue to the parameter 0.

Access to each property of an object using the parameter key:

angular.forEach(obj, function(value, key) {
    obj[key] = 0;
});
+2
source

Source: https://habr.com/ru/post/1617487/


All Articles