Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Created January 18, 2020 08:32
Show Gist options
  • Save kentcdodds/aacbf3ca4859301ed6cfabdca05f1e85 to your computer and use it in GitHub Desktop.
Save kentcdodds/aacbf3ca4859301ed6cfabdca05f1e85 to your computer and use it in GitHub Desktop.
const duplicates = filesAndHashes.filter((file1, index1, array) => {
return array.some(
(file2, index2) => index1 !== index2 && file1.hash === file2.hash,
)
})
const groups = duplicates.reduce((groups, {filepath, hash}) => {
groups[hash] = groups[hash] || []
groups[hash].push(filepath)
return groups
}, {})
// vs
const filesByHash = {}
for (const {filepath, hash} of filesAndHashes) {
filesByHash[hash] = filesByHash[hash] || []
filesByHash[hash].push(filepath)
}
const groups = {}
for (const hash in filesByHash) {
if (filesByHash[hash].length > 1) {
groups[hash] = filesByHash[hash]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment