Created
January 14, 2019 01:44
-
-
Save apolopena/212b1876eeecef9be0d1b8795c0c2503 to your computer and use it in GitHub Desktop.
JavaScript: return an array of unique numbers from an array of numbers using reduce() and find()
This file contains 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(array) { | |
return array.reduce(function(prev, num) { | |
if (!prev.find(function(item) { return item == num; })) { | |
prev.push(num); | |
} | |
return prev; | |
}, []); | |
} | |
var uniqueNumbers = makeUnique([1, 6 ,5, 6, 4, 3, 3, 3, 2, 1, 1]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment