patternjavascriptCritical
Remove duplicate values from a JavaScript array
Viewed 0 times
arrayfromremoveduplicatevaluesjavascript
Problem
I have a very simple JavaScript array that may or may not contain duplicates.
I need to remove the duplicates and put the unique values in a new array.
I could point to all the code that I've tried but I think it's useless because they don't work. I accept jQuery solutions too.
Similar question:
var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];I need to remove the duplicates and put the unique values in a new array.
I could point to all the code that I've tried but I think it's useless because they don't work. I accept jQuery solutions too.
Similar question:
- Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
Solution
Quick and dirty using jQuery:
var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];
var uniqueNames = [];
$.each(names, function(i, el){
if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
});Code Snippets
var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];
var uniqueNames = [];
$.each(names, function(i, el){
if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
});Context
Stack Overflow Q#9229645, score: 518
Revisions (0)
No revisions yet.