I had the idea to create a personalized dictionary for customers by utilizing the reduce function. Currently, I am achieving this using the forEach method.
const customers =
[ { name: 'ZOHAIB', phoneNumber: '0300xxxxx', other: 'anything' }
, { name: 'Zain', phoneNumber: '0321xxxxx', other: 'other things' }
]
let customersDictionary = {};
customers.forEach(customer => {
customersDictionary = {
...customersDictionary,
[ customer.phoneNumber ]: {name: customer.name},
};
However, my goal is to obtain the same result but with the reduce method.
customersDictionary =
{ "0300xxxxx": {"name": "ZOHAIB"}
, "0321xxxxx": {"name": "Zain"}
}