Issue Description
I am attempting to create an array utilizing the keys()
and .map()
techniques, based on a preexisting array with a length of arrLength
, so that only elements at positions which are multiples of step
are produced. See the example below,
const arrLength = 20;
const step = 5;
const newArr = [...Array(arrLength).keys() ].map((i) => i + step);
console.log(newArr) ;
// expected: 0,5,10,15,20
// received : 5,6,7,...,24
I am specifically unsure about how to increment the variable i
in the function map((i) => ... )
.
Constraints
I aim to accomplish this task in a single line using the keys
method along with Array
and possibly map
, avoiding the use of a for
loop.
Tried Approaches
I have attempted the following variations as well
Array(arrLength).fill().map((_, i) => i+step) ,
Array.from(Array(arrLength), (_, i) => i+step),
Array.from({ length: arrLength }, (_, i) => i+step)
but without success. I also tried to figure out a way to increment the i
variable using functional syntax but did not succeed either.