4

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 ) ?

1 Answer 1

11

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)
  )

2
  • 1
    Just wanted to add here, if OP doesn't want the range to include the Max value, then just set {length: MAX - MIN} without the +1.
    – Kadima
    Commented Aug 4, 2016 at 18:27
  • @Kadima. this is Good hint . thanks.. The question is good because it will facilate migration to ES6 & avoid external libraries for this issue (generate Range) Commented Aug 4, 2016 at 18:28

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.