-1

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 }));
Sar
  • 1

1 Answers1

0

You can use map to create a new array.

const arr = ['a','b','c'];
const result = arr.map(el => ({ name: el }));
console.log(result);
Andy
  • 53,323
  • 11
  • 64
  • 89