In MDN Array.prototype.reduce() , what's the meaning of allNames[name]? allNames should be {} first, but if object is empty, allNames[name] = 1 equal to {}['Alice'] = 1 ? how can it become { 'Alice': 1},
I'm so confused.
Counting instances of values in an object
var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];
var countedNames = names.reduce(function (allNames, name) {
if (name in allNames) {
allNames[name]++;
}
else {
allNames[name] = 1;
}
return allNames;
}, {});
// countedNames is:
// { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }