Last active
April 2, 2023 14:41
-
-
Save mojaray2k/f4f452f85abb181446231551cf3befdc to your computer and use it in GitHub Desktop.
The Reduce Array Method Example 5
This file contains 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
/** | |
* Reducing Properties | |
* Use the 'reduce' helper to create an object that tallies the number of sitting and standing desks. | |
* The object returned should have the form '{ sitting: 3, standing: 2 }'. | |
* The initial value has been provided to you. | |
* Hint: Don't forget to return the accumulator object (the first argument to the iterator function) | |
*/ | |
var desks = [ | |
{ type: 'sitting' }, | |
{ type: 'standing' }, | |
{ type: 'sitting' }, | |
{ type: 'sitting' }, | |
{ type: 'standing' } | |
]; | |
var deskTypes = desks.reduce((total, desk) => { | |
total.sitting += desk.type === 'sitting'; | |
total.standing += desk.type === 'standing'; | |
return total; | |
}, { sitting: 0, standing: 0 }); | |
console.log(deskTypes); | |
/** | |
* Result is | |
* [object Object] { | |
* sitting: 3, | |
* standing: 2 | |
* } | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment