Abdallah Ghrb
Abdallah Ghrb

Reputation: 193

How to generate range with MIN MAX using Array.from

Using Array.from , I can create range from 0 to N-1 as following :

var N=6;
log( Array.from({length:N},(v,k)=>k) )
<script>var log=(m)=>console.log(m)</script>

This generate [0,1,2,...,N-1]

My question is how to generate a range with Min & Max bounds in general using Array.from not something elese (Not restrict to 0 as 1st element of range ) ?

Upvotes: 4

Views: 3554

Answers (1)

Abdennour TOUMI
Abdennour TOUMI

Reputation: 93173

It is possible By identifying the length of range : MAX-MIN+1 AND identifying the first element of this range : k+MIN .

Then :

var MIN=18,MAX=23 //--> [18,19,20,21,22,23] EXPECTED
console.log(
    Array.from({length:MAX-MIN+1},(v,k)=>k+MIN)
  )

Upvotes: 11

Related Questions