I just would like to understand why this works:
let newArray = [0,1,2,3].map(elem => elem + 1); console.log(newArray);
and this doesn't:
let newArray = [0,1,2,3].map(elem => elem++); console.log(newArray);
That is related to the ++
operator.
return elem++
means return the original value of elem
, and after that, elem
value will be increased.
To fix this, use ++elem
.
Refer to ++ operator .
let newArray = [0,1,2,3].map(elem => ++elem); console.log(newArray);
if you want return elem after increase you should use ++ operation like this
let newArray = [0,1,2,3].map(elem => ++elem); console.log(newArray);
it is because you are returning the values before increment. try this
let newArray = [0,1,2,3].map(elem => ++elem); console.log(newArray);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.