javascript - Generate array of numbers between max and min value with an interval between 2 values - Stack Overflow

I have the following data structure and I need to create an array of numbers that matches given configu

I have the following data structure and I need to create an array of numbers that matches given configuration -

{
  min: 1000,
  max: 10000,
  interval: 1000
}

What would be a proper function that outputs below array -

[1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000]

It can be done through a for loop -

const output = [];
for (let i = input.min; i <= input.max; i += input.interval) {
  output.push(i);
}
console.log(output)

But I want to see if there's a cleaner way to do this using Array.fill & map -

new Array((max - min + interval)).fill(undefined).map((_, i) => (i + min))

I have the following data structure and I need to create an array of numbers that matches given configuration -

{
  min: 1000,
  max: 10000,
  interval: 1000
}

What would be a proper function that outputs below array -

[1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000]

It can be done through a for loop -

const output = [];
for (let i = input.min; i <= input.max; i += input.interval) {
  output.push(i);
}
console.log(output)

But I want to see if there's a cleaner way to do this using Array.fill & map -

new Array((max - min + interval)).fill(undefined).map((_, i) => (i + min))
Share Improve this question asked Oct 31, 2020 at 18:33 trurohittrurohit 4511 gold badge10 silver badges21 bronze badges 2
  • You're asking the exact same question twice. Why? – Andreas Commented Oct 31, 2020 at 18:39
  • @Andreas StackOverflow closed this other question and asked me to make it clearer and post it again – trurohit Commented Nov 1, 2020 at 14:59
Add a ment  | 

4 Answers 4

Reset to default 7

You can figure out how many items should be in the result by dividing the difference between max and min by the interval, then create the array with Array.from, using the mapper's index to figure out how much to add to the min for each value of the array:

const min = 1000,
  max = 10000,
  interval = 1000;

const length = (max - min) / interval + 1;
const arr = Array.from({ length }, (_, i) => min + i * interval);
console.log(arr);

Get the array's length by rounding up (max - min) / interval. Get each step's value by multiplying the current index (i) by the interval and add the min.

const fn = ({ min, max, interval }) =>
  new Array(Math.ceil((max - min) / interval)) // the array's length as a function of max, min, and interval
    .fill(undefined)
    .map((_, i) => (i * interval + min)) // a step is a product of i and interval + min


const result = fn ({
  min: 1000,
  max: 10001,
  interval: 1000
})

console.log(result)

You can use spread operator to generate the required array.

const min = 1000;
const max = 10000;
const interval = 1000;
const ret = [...Array((max - min) / interval + 1)].map(
  (_, i) => min + interval * i
);
console.log(ret);

I don't know if it's cleaner, but an interesting approach would be to use a generator. One example inspired from the Mozilla documentation

function* makeRangeIterator(start = 0, end = 100, step = 1) {
    while(start <= end) {
        yield start;
        start += step
    }
}

const sequenceGenerator = makeRangeIterator(1000, 10000, 1000);

//you have to loop through the generator
for(let value of sequenceGenerator) {
    console.log(value)
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744225695a4563993.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信