Skip to content

Instantly share code, notes, and snippets.

@ekrem-aktas
Last active September 24, 2020 12:13
Show Gist options
  • Save ekrem-aktas/41427c85441c803e1a53d60d844e30b5 to your computer and use it in GitHub Desktop.
Save ekrem-aktas/41427c85441c803e1a53d60d844e30b5 to your computer and use it in GitHub Desktop.
const basket = [
{ name:"apple", type: "fruit" },
{ name:"broccoli", type: "vegetable" }
];
// Array.reduce can be used to filter fruits
const fruits = basket.reduce((acc, food) => food.type === "fruit" ? acc.concat(food) : acc, []);
// But Array.filter is better for it
const fruits = basket.filter(food => food.type === "fruit");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment