I don't know why isnt this snippet working:
Array(50).map(e => { e = {id: Math.random() , content: 'some_content'}; return e });
It of course does what you told it to do, just not what you want it to do.
The problem is that the map function "is invoked only for indexes of the array which have assigned values, including undefined." Calling Array(50) creates 50 empty slots, but does not assign to them, so the map does nothing.
As someone else suggested, you can use Array.from() to quickly create an array of 50 elements, assigned to undefined.
undefined. So far as I know, the only way to test that difference directly is with hasOwnProperty(), but this problem demonstrates the issue indirectly.[ <10 empty slots>, 40 more... ]" whereas Firebug shows me an array of 50 undefineds. I guess it's time to ditch Firebug :Dlet x = [1,2]; x[7] = 3;. Entires 2...6 will be unassigned and skipped by map.You could use Array.apply and wrap the mapped return value in parenthesis, to return an object.
var array = Array.apply(null, { length: 50 }).map(_ => ({id: Math.random() , content: 'some_content'}));
console.log(array)
var result = Array.apply(null, { length: 50 }).map(e => { e = {id: Math.random() , content: 'some_content'}; return e });
console.log(result);
or
var result = [...Array(50)].map(e => { e = {id: Math.random() , content: 'some_content'}; return e });
console.log(result);
Array.apply(null, { length: 50 }) wont work for lengths > 500000 (in firefox)
Array.from({length: 50})asArray.from({length: 50}).map(e => ({id: Math.random() , content: 'some_content'}));.map,foreachetc. You can use spread syntax to create an iterable of n elements[...Array(50)].map()