1

I'd like to create an array of objects based on the length of another object:

let initial_array = ["a", "b", "c"]

Desired Output

[
 {width: "5%"},
 {width: "5%"},
 {width: "5%"}
]

What I've tried

I thought I could maybe use a map or forEach to create the repeating object for the length of the initial array but have failed (I'm trying to avoid using a for loop but maybe I need to?)

This doesn't work

initial_array.map(x => {width: "5%"} );
E_net4 - Krabbe mit Hüten
  • 24,143
  • 12
  • 85
  • 121
MayaGans
  • 1,649
  • 5
  • 19
  • 2
    Does this answer your question? [ECMAScript 6 arrow function that returns an object](https://stackoverflow.com/questions/28770415/ecmascript-6-arrow-function-that-returns-an-object) – Heretic Monkey Nov 02 '20 at 16:49

1 Answers1

3

Your fat arrow syntax is wrong. If you are doing an implicit return with an object you need parenthesis

const myArr = initial_array.map(x => ({width: "5%"}) );
epascarello
  • 195,511
  • 20
  • 184
  • 225