js
1const indexOn = (array, key) =>2 array.reduce((object, value) => {3 const { [key]: id, ...data } = value;4 object[id] = data;56 return object;7 }, {});89// Usage10indexOn(11 [12 { id: 1, name: 'John', age: 30 },13 { id: 2, name: 'Mary', age: 25 },14 ],15 'id',16);1718// [ { id: 1, name: 'John', age: 30 }, { id: 2, name: 'Mary', age: 25 } ]