Skip to content

Instantly share code, notes, and snippets.

@abbathaw
Created June 18, 2019 13:48
Show Gist options
  • Save abbathaw/c5c0774b28ec3e2215d3a98a46f9d1cd to your computer and use it in GitHub Desktop.
Save abbathaw/c5c0774b28ec3e2215d3a98a46f9d1cd to your computer and use it in GitHub Desktop.
separating duplicates in array
// D, C, S, H
const deck = [{number: 1, pattern: "D"}, {number: 1, pattern: "H"}, {number: 2, pattern: "A"},
{number: 1, pattern: "C"}, {number: 4, pattern: "C"}, {number: 4, pattern: "S"}]
const results = deck.reduce((acc, item) => {
if (!acc[item.number]) {
acc[item.number] = [];
}
acc[item.number].push(item);
return acc;
}, {})
console.log(results)
console.log(Object.keys(results).length)
for (let [card, pattern] of Object.entries(results)) {
console.log(`Card is ${card}`, pattern)
// DO STUFF HERE
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment