question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Array construction with spread syntax and map doesn't work

See original GitHub issue

I use this syntax quite a lot to create my Arrays

const foos =
    [...Array(4)]
        .map(x => 'foo');
// ['foo', 'foo', 'foo', 'foo']

But on your platform the array has the good size but of “undefined” element.

See here : https://stackblitz.com/edit/js-mtffex

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
sevevescommented, Jun 21, 2018

Your pattern should definitely work but how about using Array.from({ length: 4 }, x => 'foo')); 😎

0reactions
sulcocommented, Jan 25, 2021

Good catch everyone, thanks for reporting this! Yeah, it was confusing…

Thankfully it should no longer be an issue 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spread syntax (...) - JavaScript - MDN Web Docs - Mozilla
The spread (...) syntax allows an iterable, such as an array or string, to be expanded in places where zero or more arguments...
Read more >
spread syntax with map doesn't work - Stack Overflow
Try this: data.map(obj => ({person_name: obj.user.name,...obj})). The { at the beginning of the object was interpreted as the beginning of a ...
Read more >
6 Use Case of Spread with Array in JavaScript - Samantha Ming
Here are 6 ways to use the Spread operator with Array in JavaScript. You can use it to merge or clone an array....
Read more >
Rest parameters and spread syntax
It does not support array methods, so we can't call arguments.map(...) for example. Also, it always contains all arguments. We can't capture ...
Read more >
Spread operator, array.map, and initializing nested arrays
In your first example, the unfilled array is empty, so the map function doesn't find anything to work with. But instead of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found