Code sample

Quickly creating and mapping an array in JavaScript

When, for instance, creating test data one might do something like this:

const data = [];
for(let i = 0; i < 100; i++) {
data.push({ num: i });
}

This may be written slightly shorter and more wrist friendly using Array.fill:

const data = Array(100).fill(null).map((val, i) => {
return { num: i };
});

 

Joel Abrahamsson

Joel Abrahamsson

I'm a passionate web developer and systems architect living in Stockholm, Sweden. I work as CTO for a large media site and enjoy developing with all technologies, especially .NET, Node.js, and ElasticSearch. Read more

More about Programming