Javascript Js 为每个循环更改数组内的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33479866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:07:24  来源:igfitidea点击:

Js change object inside array in for each loop

javascript

提问by user233232

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);

回答by T.J. Crowder

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.

它不起作用,因为您所做的只是更新您获得的参数 ( item)的值,该值与数组没有实时连接。一旦您的回调返回,该更改就会消失。

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

最合适的方法是使用map

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

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

console.log(arr);

mapgives your function each item and builds a newarray from whatever you return.

map为您的函数提供每个项目,并根据您返回的任何内容构建一个数组。

If it's important that you update the array in-place instead of building a new one, you canuse 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:

如果就地更新数组而不是构建新数组很重要,则可以使用forEach,您只需要将其分配回要更新的数组元素。forEach的回调的第二个参数是您正在访问的索引,因此:

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 itemfor 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 answershows you how to do that.

当然,在上述两种情况下,你会被实际使用item的东西,你是不是在你的示例代码。如果你想添加/删除属性上item,不能完全替代的对象,西里尔的回答展示了如何要做到这一点。

回答by Cyril Cherian

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
});

回答by rajuGT

You are changing the local reference itemin the callback function.

您正在更改item回调函数中的本地引用。

To change array contents, you need to use the array index and assign new reference to it, as shown in below

要更改数组内容,您需要使用数组索引并为其分配新的引用,如下所示

arr.forEach(function(item, i) {
  arr[i] = {somethingElse: 1} //using index, change the reference in array
});

回答by epascarello

You are not working on the object so changes are not transferred.

您不是在处理对象,因此不会传输更改。

You can do this instead:

你可以这样做:

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

回答by Sebastián Vansteenkiste

In a related note, if you're looking for how to change a propertyof the objects in the array, keep in mind you can just operate on each on your forEachloop and the changes will stick:

在相关说明中,如果您正在寻找如何更改数组中对象的属性,请记住,您可以对forEach循环中的每个对象进行操作,并且更改将保持不变:

let myArray = [{}];
myArray.forEach((obj) => {obj.foo = "bar";});
console.log(myArray[0].foo); //"bar"