I have an array of branches that looks roughly like this:
let branches = [
{
id: 21,
name: "Branch 1",
opening_times: [ {}, {}, {} ] // Array of objects (Monday, Tuesday etc)
},
{
id: 22,
name "Branch 2"
opening_times: [ {}, {}, {} ] // Array of objects (Monday, Tuesday etc)
},
// .. etc
]
But I'd like to turn it into an object with the name as the key for each.
Desired output:
branches = {
"Branch 1": {
id: 21,
opening_times: [ {}, {}, {} ] // Array of objects (Monday, Tuesday etc)
},
"Branch 2": {
id: 22,
opening_times: [ {}, {}, {} ] // Array of objects (Monday, Tuesday etc)
}
}
Tried:
let newBranches = branches.map(branch => (
{
[branch.name]: {
id: branch.id,
days: branch.opening_times
}
}
));
console.log(newBranches)
But of course mapping gives me an array output:
[
0: {Branch 1: {…}}
1: {Branch 2: {…}}
]
Can anyone help point me in the right direction to get a new object with the name key as an object itself?