Skip to main content

Javascript Data Operations

Object Array to an Object lead by key

Transfer Object Array into an Object lead by id from the items' value

// Input
const input = [
{
"key": "uuid-1",
"value": "value1"
},
{
"key": "uuid-2",
"value": "value2"
},
{
"key": "uuid-3",
"value": "value3"
}
]

const output = input.reduce((obj, item) => ({
...obj,
[item.key]: item.value
}), {})

// Output
// {
// "uuid-1": "value1",
// "uuid-2": "value2",
// "uuid-3": "value3"
// }