0

how to convert this:

var arr = ['a','b','c']

to this

arr = [{name: 'a'}, {name: 'b'}, {name: 'c'}]

I already tried this code

arr.forEach((key,name) => Object.assign(obj, { name: key }));
2
  • 1
    arr.forEach( (key,name) => arr[key] = { name: name } ); Commented Jun 4, 2021 at 8:58
  • 2
    const output = arr.map(name => ({ name })) Commented Jun 4, 2021 at 9:00

1 Answer 1

2

You can use map to create a new array.

const arr = ['a','b','c'];
const result = arr.map(el => ({ name: el }));
console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.