DEV Community

Discussion on: Array Transformations With `reduce()` Method

Collapse
 
davidyaonz profile image
David Yao

hi, just for the reference, the correct code for reducing count of instances is :
['a', 'b', 'a', 'c', 'b'].reduce((acc, value) =>
acc[value]
? { ...acc, [value]: acc[value]+1 }
: { ...acc, [value]: 1 },
{});

Cheers