I have an array like this:
const peopleArray = [
{
"id": "Antoine",
"country": "France"
},
{
"id": "Alejandro",
"country": "Spain"
}
]
That I would like to represent as on object like this (note that id is not a property):
{
"Antoine": {
"country": "France"
},
"Alejandro": {
"country": "Spain"
}
}
So far I've found I can do this (elegant!):
peopleArray.reduce( (ac, p) => ({...ac, [p.id]: p }), {} )
Which produces:
{
"Antoine": {
"id": "Antoine",
"country": "France"
},
"Alejandro": {
"id": "Alejandro",
"country": "Spain"
}
}
I'm at a loss of how to accomplish the same in a terse/elegant way such that id
is omitted.
Looking for a pure javascript es2017 solution that works with Node.js version 8.11.1.