如何在 Javascript .map() 函数中改变原始数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46520833/
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-10-29 06:43:43  来源:igfitidea点击:

How to mutate original array in Javascript .map() function?

javascript

提问by Maxgrid

For eg:

例如:

var persons = [{ "name":"A", "salary":1200 }, { "name":"B", "salary":"1500" }];

And you want to change the value of "salary" of each person in an original array.

并且您想更改原始数组中每个人的“薪水”值。

回答by Suren Srapyan

If you want to mutate the original array, you can use Array#forEachfunction.

如果你想改变原始数组,你可以使用Array#forEach函数。

const persons = [{ "name":"A", "salary":1200 }, { "name":"B", "salary": 1500 }];

persons.forEach(item => item.salary += 1000);

console.log(persons)

Array#mapcreates a new array of the created items and returns that. After you need to assign the returned result.

Array#map为创建的项目创建一个新数组并返回它。之后你需要分配返回的结果。

let persons = [{ "name":"A", "salary":1200 }, { "name":"B", "salary": 1500 }];

persons = persons.map(item => {
  item.salary += 1000;
  return item;
});

console.log(persons);

回答by terreb

You can mutate the objects directly iterating with map. If I understood you correctly.

您可以直接使用 map 迭代来改变对象。如果我理解正确的话。

persons.map(i => { i.salary = i.salary * 1.25; return i; });
console.log(persons);
// [{ "name":"A", "salary": 1875 }, { "name":"B", "salary": 2343.75 }]

回答by marvel308

you can use a simple for loop for that

你可以使用一个简单的 for 循环

var persons = [{ "name":"A", "salary":1200 }, { "name":"B", "salary":"1500" }];

for(let element of persons){
    element.salary*=2;
}

console.log(persons);

回答by keskinsaf

I read other answers, you can use any of them, but I see some problems there.

我阅读了其他答案,您可以使用其中任何一个,但我发现那里存在一些问题。

I will mention 2 methodologies I have used in many different languages, mapand forEach. mapis a functional way of traversing a collection and creating some new collection with new (different or same) elements, independent of languages. With map, it is expected to create a new collection that is created by some mappingfrom initial collection. On the other hand, forEachis a method that eases traversing a collection by not using usual forloop syntax for collections, and mutating (or changing) each item if desired.

我将提到我在许多不同语言中使用的 2 种方法,map以及forEach. map是一种遍历集合并创建一些具有新(不同或相同)元素的新集合的功能方式,与语言无关。使用map,预计会创建一个mapping由初始集合中的某些人创建的新集合。另一方面,forEach是一种通过不使用集合的常用for循环语法并根据需要改变(或更改)每个项目来简化遍历集合的方法。

If you use mapon a collection that contains objects, and change those objects in the mapperfunction, you might face with unexpected behavior. Beacuse you are changing directly the object you are operating on, and do not mappingit to another object. This object might can be considered as a stateand computers works based on the state transfers. If you want to change that object, i.e. some state, it is absolutely ok, but based on the description, you should not use map for such a case. Because you are not creating a new array with some new values, but instead, mutating provided elements. Use forEachfor such a case.

如果map在包含对象的集合上使用,并在mapper函数中更改这些对象,则可能会遇到意外行为。因为您正在直接更改您正在操作的对象,而不是将mapping其更改为另一个对象。这个对象可以被认为是一个state基于状态转移的计算机工作。如果你想改变那个对象,即一些状态,这绝对没问题,但根据描述,你不应该在这种情况下使用 map。因为您不是使用一些新值创建新数组,而是更改提供的元素。对于这种情况,请使用forEach

I have added an example here. You can click the link and take a look at the console, and see my what I mean in a more clear way.

在这里添加了一个例子。您可以单击链接并查看控制台,并以更清晰的方式了解我的意思。

As far as I know, based on my experience, mutations in mapmethod is considered as bad practice and discouraged.

据我所知,根据我的经验,map方法的突变被认为是不好的做法并且不鼓励。

These two are added for different purposes and it would be better to use them as expected.

添加这两个是出于不同的目的,最好按预期使用它们。

For more, see Mozilla Web Docs page for Array.

有关更多信息,请参阅Array 的 Mozilla Web 文档页面

回答by Miguel Coder

var persons = [{ "name":"A", "salary":1200 }, { "name":"B", "salary":"1500" }];

var mutatedPersons = persons.map(function(obj){
   return {name:obj.name,salary:parseInt(obj.salary) + 100};
})

console.log(mutatedPersons);

回答by Maxgrid

.map() function takes third parameter in its callback thats the instance of original array.

.map() 函数在其回调中采用第三个参数,即原始数组的实例。

You could do something like this also:

你也可以做这样的事情:

var persons = [{ "name":"A", "salary":1200 }, { "name":"B", "salary":1500 }];

persons.map(function(person, key, array) {
  array[key].salary *= 2;
});

console.log(persons);