I have two arrays as follow:
var Ids = ['123', '456', '789', '789'];
var Names = ['jack', 'jack', 'peter', 'peter'];
Now I need to remove duplicate values in the Ids array and then according to that, remove values in the Names array.
I can remove duplicate values for each array separately using .filter() like this:
var Unique_Ids = Ids.filter(function(item, pos) { return Ids.indexOf(item) == pos; });
//=> 123, 456, 789
var Unique_Names = Ids.filter(function(item, pos) { return Names.indexOf(item) == pos; });
//=> jack, peter
But that isn't what I need .. I need the number of items in both arrays to be equal (in this case 3, according to the number of items in Unique_Ids).
Anyway what I need is this:
var Unique_Ids = ['123', '456', '789'];
var Unique_Names = ['jack', 'jack', 'peter'];
How can I do that?
Note: I don't want a solution containing this indexOf() method. Because it doesn't work on IE and old browsers. And I think jQuery.inArray() can be a good alternative.
array.filter, you havearray.indexOfarray.indexOfworks in IE9+ - do you really need to support IE8 or earlier?