Created
April 6, 2021 12:28
-
-
Save nhatnx/a3d4ddb04ca242d0fc697f58ba5c4b37 to your computer and use it in GitHub Desktop.
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
//// ES 5 | |
function onlyUnique(value, index, self) { | |
return self.indexOf(value) === index; | |
} | |
// usage example: | |
var a = ['a', 1, 'a', 2, '1']; | |
var unique = a.filter(onlyUnique); | |
console.log(unique); // ['a', 1, 2, '1'] | |
//// ES 6 | |
var myArray = ['a', 1, 'a', 2, '1']; | |
var unique = myArray.filter((v, i, a) => a.indexOf(v) === i); | |
console.log(unique); // unique is ['a', 1, 2, '1'] | |
//// ES 6 use native object Set | |
var myArray = ['a', 1, 'a', 2, '1']; | |
let unique = [...new Set(myArray)]; | |
console.log(unique); // unique is ['a', 1, 2, '1'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment