+ 2
What is the output? And why?
let x = [1, 2, 3] x.map(v => v * 2) alert(x[2])
3 ответов
+ 8
3
x is unmodified. Because map creates new copy, won't affect original x.
To modify, need to store back result, if you want as x = x.map( v => v*2)
+ 7
You can find out _what_ the output is by running the code.
Why does it turn out like this? – map() does not work inplace, it creates a new array.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
0
Daniel 😊😎😉[JS Challenger]
you could force it to do an in-place modification
let x = [1, 2, 3]
console.log(x)
x.map((_,i) => x[i]*=2)
console.log(x)
alert(x[2])