Skip to content

Instantly share code, notes, and snippets.

@manavm1990
Created February 22, 2020 13:39
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 manavm1990/7a3858a2718450e8ad36b9b9180868d2 to your computer and use it in GitHub Desktop.
Save manavm1990/7a3858a2718450e8ad36b9b9180868d2 to your computer and use it in GitHub Desktop.
Given an Array of Objects, determine how many of the Objects contain a specified key
const countKeys = (data, key) => {
return data.reduce((total, datum) => (total += datum[key] ? 1 : 0), 0);
};
@manavm1990
Copy link
Author

This is probably not that useful outside of another example of reduce. After all, we might as well just use a map to actually get the values of that key ☝️ ...not just count it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment