1

Problem Description

I am trying to generate an array using keys() and .map() methods, given a preexisting array with length = arrLength, such that only items at a position that are multiples of step are generated. Here is an example,

const arrLength = 20;
const step = 5;

const newArr = [...Array(arrLength).keys() ].map((i) => i + step);

console.log(newArr) ;
// expected: 0,5,10,15,20
// recieved : 5,6,7,...,24

I am particlarly confused as to how to increment the left hand side variable i in map((i) => ... ).

Restrictions

I want to implement this in one line using the keys method and Array and possibly map, so no for loop.

Things I have tried

I tried the following signatures instead

    Array(arrLength).fill().map((_, i) => i+step) ,
    Array.from(Array(arrLength), (_, i) => i+step),
    Array.from({ length: arrLength }, (_, i) => i+step)

but with no success. I have further tried to find a way to increment the i variable using functional syntax but with no success as wel.

1
  • 1
    I think you should try i*step not + Commented Sep 28, 2021 at 5:01

4 Answers 4

3

const arrLength = 20;
const step = 5;
const arr = [];
for (let i = 0; i<= arrLength; i+=step){
  arr.push(i);
}
console.log(arr);

Or

const arrLength = 20;
const step = 5;

const newArr = [...Array(Math.floor(arrLength/step) + 1).keys() ].map((i) => i * step);
console.log(newArr);

Sign up to request clarification or add additional context in comments.

2 Comments

No sorry I cannot use a for loop, I need to do it in one line with keys() method and Array. I will clarify in the post.
@hexaquark I add another one.
2

range function work with ES4 Using Array.from can pass an object has property length in it

const range = (start, stop, step) => Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step));
console.log(range(0, 20, 5))

1 Comment

This does not work for ranges in the negative space
0

Stumbled upon this, and while looking at the answers I came up with this. This just basically creates an array with incrementing numbers, but could be extended easily.

This reduce function generates desired array by taking the index i of given element and then we combine what we already got in the previous recursive step (the a variable) with the incremental value i.

const arr = new Array(52).fill(0).reduce((a,_,i) => [...a, i],[])

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

I know this is old post but why not just this?

const step = 5;
const result = [...Array(10).keys()].map( i => i * step );

console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.