Array construction with spread syntax and map doesn't work
See original GitHub issueI 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:
- Created 5 years ago
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Your pattern should definitely work but how about using
Array.from({ length: 4 }, x => 'foo'));
😎Good catch everyone, thanks for reporting this! Yeah, it was confusing…
Thankfully it should no longer be an issue 😃