Skip to content

Instantly share code, notes, and snippets.

@nhatnx
Created April 6, 2021 12:28
Show Gist options
  • Save nhatnx/a3d4ddb04ca242d0fc697f58ba5c4b37 to your computer and use it in GitHub Desktop.
Save nhatnx/a3d4ddb04ca242d0fc697f58ba5c4b37 to your computer and use it in GitHub Desktop.
//// 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