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

1 Answers1

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);
Andy
  • 61,948
  • 13
  • 68
  • 95