array.map不会更改数组值

时间:2019-10-26 20:41:36

标签: javascript

为什么array.map不起作用?

我的代码是:

let myArray = [000,111,222,333,444,555,666,777,888,999];

myArray.map((value) => {
     return = 1000 - value;
});

console.log(myArray);

结果是: [0,111,222,333,444,555,666,777,888,999]

1 个答案:

答案 0 :(得分:3)

调用map返回一个新数组。它不会在原位置修改旧的。改为这样做:

let myArray = [000,111,222,333,444,555,666,777,888,999];

let myNewArray = myArray.map((value) => {
     return 1000 - value;
});

console.log(myNewArray);