How To Convert An Array To Array Of Object In Javascript
I want to convert an Array like: [ 'John', 'Jane' ] into an array of object pairs like this: [{'name': 'John'}, {'name':'Jane'}] Please help me to do so..
Solution 1:
Try the "map" function from the array:
const output = [ 'John', 'Jane' ].map(name => ({name}));
console.log(output);
Solution 2:
You can use the instance method .map() on a JS list Object as follows :
let list = ['John', 'Jane']
list = list.map(x => {
return({name: x});
});
console.log(list);
Post a Comment for "How To Convert An Array To Array Of Object In Javascript"