Q:

Array > Object: how to index an array of objects with reduce, omitting the index from nested element in result (es2017)?

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.

A:

If you want to make and object with id as key and remaining object as value. You can try following using Rest Parameters
ES2017

const peopleArray = [{"id": "Antoine","country": "France"},{"id": "Alejandro","country": "Spain"}];

const result = peopleArray.reduce( (ac, o) => { 
  ac[o.id] = Object.assign({}, o); 
  delete ac[o.id].id; 
  return ac; 
}, {});
console.log(result);

ES2018 - Will be able to use Rest Parameters for objects

const peopleArray = [{"id": "Antoine","country": "France"},{"id": "Alejandro","country": "Spain"}];

const result = peopleArray.reduce( (ac, {id, ...rest}) => Object.assign(ac, {[id]: rest}), {} );
console.log(result);

