DEV Community

Tim Pozza
Tim Pozza

Posted on

Map object keys to vars and populate from values

Template literals to the rescue, doing things tap dancing in the neighbourhood had already forbidden!

For the codepen, use 'global' in place of 'window' for Node, as Node has no window object, though I do wonder about server side headless rendering with the likes of rendertron, and others. Perhaps someone here has a perspective on it.

Also, for your consideration, as the left hand side shows destructuring doing both calculation and assignment:

soapand = [
    {name: 'far',
    email: 'quad'},
    {name: 'car',
    email: 'squad'}
]

// let {name: `${'wingedAngel'}`, email} = soapand
// global[`wingedAngel`] = `${}`
refreshed = soapand.map((item, idx) => {
    // console.log(item)
    window[`wingedAngel`] = `${item.name}`
    mad = ['holyWater']
    window[`${mad[0]}`] = item.email
    // window['holyWater'] = item.email
    return wingedAngel + ' ' + holyWater
})

// window[`wingedAngel`] = `${soapand[0].name}`
// window['holyWater'] = soapand.email
console.log(refreshed)
console.log(wingedAngel)
console.log(holyWater)

I was pretty happy about finding an obscure one line reference to this in MDN documentation. I hope this bit of movie magic helps you in some way.

Top comments (0)