I wanted to create a customer's dictionary using reduce function, I am doing it using forEach
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},
};
I wanted the same output but using reduce method.
customersDictionary =
{ "0300xxxxx": {"name": "ZOHAIB"}
, "0321xxxxx": {"name": "Zain"}
}