Jane Emelia Jane Emelia - 2 months ago 19
Javascript Question

use property value as property key using map

I want to return a new array but take the property value to become property name.

const temp = [
{name: 'james'},
{name: 'ally'}
]

const new = temp.map(obj => ({
`${obj.name}`: null
}))


Obviously it doesn't work this way. Any clue? https://jsfiddle.net/qg8ofom1/

Answer Source

const temp = [
  {name: 'james'},
  {name: 'ally'}
];

const newObj = temp.map(obj => ({
    [obj.name]: null
}));

console.log(newObj);