I have an array with a specified length and I'm trying to populate it with values that are dependent on each index.
let arr = new Array(someLength)
arr.map((v, i) => i * 2)
From what I know, this isn't working because map
skips undefined values.
I have a few questions:
- Why does
map
work on something like[undefined, undefined]
? Is there anyway to accomplish this using ES6 array methods?
I know I can use a standard
for
loop, but was wondering if there's a nicer way to do it.for (let i = 0; i < arr.length; i++) { arr[i] = i * 2 }
I've found one method so far, it's not really clean though.
arr = arr.fill(undefined).map((foo, i) => i * 2)