Last active
August 2, 2018 23:06
-
-
Save funador/f599d6d1ff4ca453c31e59d630f855b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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