Jane Emelia Jane Emelia - 7 months ago 47
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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download