Skip to content

Instantly share code, notes, and snippets.

@mojaray2k
Last active April 2, 2023 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mojaray2k/f4f452f85abb181446231551cf3befdc to your computer and use it in GitHub Desktop.
Save mojaray2k/f4f452f85abb181446231551cf3befdc to your computer and use it in GitHub Desktop.
The Reduce Array Method Example 5
/**
* 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