Skip to content

Instantly share code, notes, and snippets.

@funador
Last active August 2, 2018 23:06
Show Gist options
  • Save funador/f599d6d1ff4ca453c31e59d630f855b1 to your computer and use it in GitHub Desktop.
Save funador/f599d6d1ff4ca453c31e59d630f855b1 to your computer and use it in GitHub Desktop.
const vetApprovedDogs = [
{name: 'Snookums', needsShots: false, breed: 'pug', age: 2},
{name: 'Thor', needsShots: false, breed: 'hound', age: 1},
{name: 'Wunderbar', needsShots: false, breed: 'pug', age: 9},
]
// The big report for the city, have to be sure we get this right!
const reportForCity = vetApprovedDogs.myReduce((report, dog) => {
// Is this breed not in the report?
if(!report[dog.breed]) {
// Make a note that we have seen it once
report[dog.breed] = 1
}
else {
// We have already seen this breed, so just add one to the total
report[dog.breed] ++
}
// Return the updated report to use again!
return report
// Empty Object as second argument **and** the first iteration of 'report'
}, {})
console.log(reportForCity) // { pug: 2, hound: 1 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment