最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to use Array.fill to create 0...499 in JavaScript? - Stack Overflow

programmeradmin0浏览0评论

I want to use Array.fill method to create a sequence from 0...499 like

const arr = []
for(let i = 0; i < 500; i++)
   arr.push(i);

Currently, I am using Array.from like:

const arr = Array.from(Array(500).keys())

But how can I use just Array.fill to create the sequence? Is it even possible?

I want to use Array.fill method to create a sequence from 0...499 like

const arr = []
for(let i = 0; i < 500; i++)
   arr.push(i);

Currently, I am using Array.from like:

const arr = Array.from(Array(500).keys())

But how can I use just Array.fill to create the sequence? Is it even possible?

Share Improve this question asked Jul 5, 2019 at 16:46 deadcoder0904deadcoder0904 8,73318 gold badges86 silver badges208 bronze badges 6
  • 6 I don't think you can. Array.fill fills the array with the same values, not different values. – Barmar Commented Jul 5, 2019 at 16:51
  • 1 My counter question would be "why?" – user5734311 Commented Jul 5, 2019 at 16:53
  • 2 You can't DOC says "The fill() method fills (modifies) all the elements of an array from a start index (default zero) to an end index (default array length) with a static value. It returns the modified array". – Maheer Ali Commented Jul 5, 2019 at 16:53
  • 6 Just use Array.from, i.e. Array.from({ length: N }, (v, i) => i) – Terry Commented Jul 5, 2019 at 16:54
  • 1 @Terry He knows the other methods. That's why I didn't close this as a dupe of stackoverflow./questions/55579499/… – Barmar Commented Jul 5, 2019 at 16:55
 |  Show 1 more ment

4 Answers 4

Reset to default 6

No you can't do it with fill alone as fill method adds a static value

The fill() method fills (modifies) all the elements of an array from a start index (default zero) to an end index (default array length) with a static value. It returns the modified array.

console.log(new Array(500).fill(0).map((_,i) => i ))
console.log(Array.from(Array(500).keys()))

It's not possible to use fill alone to do what you ask. You'll have to bine it with one or more other methods.

I can't say I remend this approach, because it really doesn't feel like idiomatic JavaScript, but just for demonstration purposes, here's a solution that uses fill to populate the resulting array (instead of just using it to get around the awkwardness of the Array constructor). It's also more verbose and probably less performant than the alternatives.

console.log(Array.from({ length: 500 }).reduce((x, y, i, a) => (a.fill(i, i, i + 1), a), []))

For what it's worth, I find Array.from({ length: 500 }, (_, i) => i) as @Terry suggested to be a much more elegant.

You can use a one-line helper like this:

function createArrayWithNumbersFromRange(start: number, end: number): number[] {
  return [...Array(end - start + 1)].map((_, index) => start + index);
}

You could generate an Iterator and push it to a new array.

[...Array(5).keys()];
 => [0, 1, 2, 3, 4]
发布评论

评论列表(0)

  1. 暂无评论