I have an array of elements I want to map to a new array of objects with a key as a name.
let array = ["abc", "def", "xyx"]
Expected Output
let array1 = [{name: "abc"}, {name: "def"}, {name: "xyz"}]
I have an array of elements I want to map to a new array of objects with a key as a name.
let array = ["abc", "def", "xyx"]
Expected Output
let array1 = [{name: "abc"}, {name: "def"}, {name: "xyz"}]
let arr = ['a', 'b', 'c'];
let newArr = arr.map(el => ({ name: el }));
console.log(newArr);
you can use map() function for that,
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
let array = ["abc", "def", "xyx"]
const array1 = array.map( data => {
return {name:data}
})
/* this code will also work,if you dint specify the properties name
by default it will be the variable's name. In this case 'name' */
const array2 = array.map( name => {
return {name}
})
console.log(array1)
console.log(array2)