Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create array of integers between two numbers in Javascript

I am trying to create an array that begins at 0.05 and ends at 2.5. I want the values in between the min and max to grow by increments of 0.245.

var min = 0.05;
var max = 2.5;
var increments = ((max - min) / 100);
var arr = [];

the final output should be like this:

[0.05, 0.0745, 0.99, 0.1235, 0.148 ... 2.5]
like image 297
Bwyss Avatar asked Dec 04 '25 10:12

Bwyss


1 Answers

using ES6/ES2015 spread operator you can do this way:

let min = 0.05,
    max = 2.5,
    items = 100,
    increments = ((max - min) / items);

let result = [...Array(items + 1)].map((x, y) => min + increments * y);

if you need to round your numbers to x number of digits you can do this way:

let result = [...Array(items + 1)].map((x, y) => +(min + increments * y).toFixed(4));

Here is an example of the code

like image 63
Vlad Bezden Avatar answered Dec 05 '25 23:12

Vlad Bezden



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!