4

I have the object :

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

I want to set zero to every object properties using angular.forEach():

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

I displayed their values using console.log() and I found out that none of them are zero.

but when I do

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

Their values are zero.

Can anyone explain about this?

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Cheetah Felidae
  • 999
  • 2
  • 7
  • 14
  • 1
    You're just assigning the parameter. You can't actually do that. – SLaks Nov 25 '15 at 16:27
  • 1
    See http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language and http://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value. – JKillian Nov 25 '15 at 16:30

1 Answers1

2

You are just assigning the value parameter a value of 0.

Access each property on the object with the key parameter:

angular.forEach(obj, function(value, key) {
    obj[key] = 0;
});
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304