1const toObject = (arr, key) => arr.reduce((a, b) => ({ ...a, [b[key]]: b }), {});
2
3// Example
4toObject(
5 [
6 { id: '1', name: 'June', gender: 'Female' },
7 { id: '2', name: 'Alex', gender: 'Male' },
8 { id: '3', name: 'Harry', gender: 'Male' },
9 ],
10 'id'
11);
12/*
13{
14 '1': { id: '1', name: 'June', gender: 'Female' },
15 '2': { id: '2', name: 'Alex', gender: 'Male' },
16 '3': { id: '3', name: 'Harry', gender: 'Male' },
17}
18*/