0

I want to generate an array with many elements inside.

const a = Array.from({length: 5}, (v, i) => {car:5});
console.log(a)

With the code above, i want to get this:

[
{car:5},
{car:5},
{car:5},
{car:5},
{car:5}
]

...but now i get undefined. It is possible to get what i described using Array.from()?

Asking
  • 3,487
  • 11
  • 51
  • 106

1 Answers1

1

If you want to return object from arrow function, wrap it into curly bracket ()

const a = Array.from({length: 5}, (v, i) => ({car:5}));
console.log(a)
hgb123
  • 13,869
  • 3
  • 20
  • 38