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%"} );
1

1 Answer 1

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%"}) );
Sign up to request clarification or add additional context in comments.

1 Comment

You're the one with the gold badge, not me. But hey, you got yours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.