Created
May 23, 2018 15:49
-
-
Save berdosi/6b7abc1fdc3e8bdcab0df0c5b794cde8 to your computer and use it in GitHub Desktop.
Remove duplicates from an array (to be used in Array.reduce()). If array.length may be 1, an emty array needs to be supplied as an initial value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeUnique(prev, current, index) { | |
return (index === 1 | |
? [prev].concat(prev !== current ? current : []) // handle when first two items are identical | |
: (prev.indexOf(current) > -1 | |
? prev | |
: (prev.push(current), prev))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment