Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Js change object inside array in for each loop

Tags:

javascript

I want to change the current object in for each loop and it does not work, Why it is not working and how can i do this?

var arr = [{num: 1}, {num: 2}];

arr.forEach(function(item) {
  item = {somethingElse: 1}
});

console.log(arr);
like image 848
user233232 Avatar asked Nov 02 '15 14:11

user233232


People also ask

Can you modify array in forEach?

Note that foreach does not modify the internal array pointer, which is used by functions such as current() and key(). It is possible to customize object iteration. In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

How do you change an object in an array?

To change the value of an object in an array:Call the findIndex() method to get the index of the specific object. Access the array at the index and change the property's value using dot notation. The value of the object in the array will get updated in place.

How do you change each element in an array?

To change the value of all elements in an array:Use the forEach() method to iterate over the array. The method takes a function that gets invoked with the array element, its index and the array itself. Use the index of the current iteration to change the corresponding array element.

Can the elements of an array be updated using a forEach loop?

forEach does not modify the array itself, the callback method can. The method returns undefined and cannot be chained like some other array methods. forEach only works on arrays, which means you need to be a little creative if you want to iterate over objects.


2 Answers

It's not working because all you're doing is updating the value of the argument you were given (item), which doesn't have a live connection to the array. That change disappears as soon as your callback returns.

The most appropriate way to do this is to use map:

var arr = [{num: 1}, {num: 2}];

arr = arr.map(function(item) {
  return {somethingElse: 1};
});

console.log(arr);

map gives your function each item and builds a new array from whatever you return.

If it's important that you update the array in-place instead of building a new one, you can use forEach, you just have to assign back to the array element you're updating. The second argument to forEach's callback is the index you're visiting, so:

var arr = [{num: 1}, {num: 2}];

arr.forEach(function(item, index) {
  arr[index] = {somethingElse: 1};
});

console.log(arr);

Of course, in both cases above, you'd actually be using item for something, which you aren't in your example code... If you wanted to add/remove properties on item, not replace the object entirely, Cyril's answer shows you how to do that.

like image 199
T.J. Crowder Avatar answered Oct 09 '22 05:10

T.J. Crowder


Another variety to the list of answers

var arr = [{num: 1}, {num: 2}];

arr.forEach(function(item) {
  item.something = 2;//setting the value
  delete item.num;//deleting the num from the object
});
like image 41
Cyril Cherian Avatar answered Oct 09 '22 07:10

Cyril Cherian