Filter duplicate values from array

function onlyUnique(value, index, self) { return self.indexOf(value) === index; } // usage example: var a = ['a', 1, 'a', 2, '1']; var unique = a.filter( onlyUnique ); // returns ['a', 1, 2, '1']
A way to filter duplicate values from array. I keep forgetting how to do it, so decided to write it here, so I can check it out quickly in the future. I did not create this snippet, I copied from Stackoverflow ( http://stackoverflow.com/questions/1960473/unique-values-in-an-array), which also has a detailed description of the solution.... so, in short, credit where credit is due!

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.