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()?

AskMen
  • 1,915
  • 2
  • 11
  • 27

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
  • 11,887
  • 3
  • 15
  • 33