Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 288
Abdallah Ghrb Avatar asked Oct 16 '25 04:10

Abdallah Ghrb


1 Answers

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)
  )
like image 171
Abdennour TOUMI Avatar answered Oct 17 '25 19:10

Abdennour TOUMI



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!