Skip to content

Instantly share code, notes, and snippets.

@adamgiese
Created July 17, 2018 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamgiese/3bf0dd8edf00a2de4db9d699d90d8888 to your computer and use it in GitHub Desktop.
Save adamgiese/3bf0dd8edf00a2de4db9d699d90d8888 to your computer and use it in GitHub Desktop.
Declarative Arrays: Restaurant Reducer
const currentTime = 15; // 3:00 PM
const toOpenRestaurants = (openRestaurants, restaurant) => {
const {
name,
cuisine,
hours: {
open,
close,
}
} = restaurant;
const isOpen = currentTime > open && currentTime < close;
const isFood = cuisine !== 'Coffee';
return isFood && isOpen ? [...openRestaurants, name] : openRestaurants;
};
const openRestaurants = restaurants.reduce(toOpenRestaurants, []);
console.log(openRestaurants); // ["Pizza Planet", "Bob's Burgers", "Monks Cafe"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment