Skip to content

Instantly share code, notes, and snippets.

@itacirgabral
Created December 14, 2018 19:01
Show Gist options
  • Save itacirgabral/5d42858be6c36f4e924f4ea85efb490f to your computer and use it in GitHub Desktop.
Save itacirgabral/5d42858be6c36f4e924f4ea85efb490f to your computer and use it in GitHub Desktop.
const partitions = arr => arr.reduce((map, obj) => Object.keys(obj).reduce((map, key) =>{
if (map.has(key)) {
map.set(key, map.get(key) + 1)
} else {
map.set(key, 1)
}
return map
}, map), new Map())
export default partitions
/*
partitions([
{"key1": "value1", "key2": "value2"},
{"key1": "value1", "key3": "value3"}
])
{"key1" => 2, "key2" => 1, "key3" => 1}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment