If I wanted to turn this data structure:
time = [
{id: 'actual', summed: 24000},
{id: 'plan', summed: 8000}
]
in the following data structure using JavaScript or lodash, what would be the best approach?
time = {
actual: 24000,
plan: 8000
}
Thanks in advance for the help!
CodePudding user response:
let time = [{ id: 'actual', summed: 24000 }, { id: 'plan', summed: 8000 }]
let obj = time.reduce((acc, {id, summed}) => ({...acc, [id] : summed}),{})
console.log(obj)
CodePudding user response:
You could map the wanted key/value pairs and build an object from it.
const
time = [{ id: 'actual', summed: 24000 }, { id: 'plan', summed: 8000 }],
result = Object.fromEntries(time.map(({ id, summed }) => [id, summed]));
console.log(result);