I have an array of objects and an forEach()
, like this one:
$scope.things = [
{x: 2},
{x: 4},
{x: 5}
];
angular.forEach($scope.things, function(thing) {
//Pick one of the lines at a time
//thing.x += 10; // <--- works
//thing.k = thing.x + 1; // <--- works
//thing = {k: thing.x + 10, o: thing.x - 1}; // <--- doesn't work
//
});
console.log($scope.things);
And by "works", "works" and "doesn't work"
I mean:
x
is added 10, and final array looks as{x: 12}, {x: 14}, {x: 15}
k
is created, and final array looks as{x: 2, k: 3}, {x: 4, k: 5}, {x: 5, k: 6}
thing
itself isn't replaced, and the array ofthings
looks exactly like at the beginning.
My questions are:
- Why is this happening?
- Is that the expected behavior?
- How can I completely replace each
thing
?