stack
stack

Reputation: 10228

Remove Duplicates from JavaScript Array According to another Array?

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.

Upvotes: 1

Views: 100

Answers (5)

Rudy de la Garza
Rudy de la Garza

Reputation: 1

You can loop through the first array and fill new arrays every time a new item is found:

var Ids   = ['123',  '456',  '789',   '789'];
var Names = ['jack', 'jack', 'peter', 'peter'];
var Unique_Ids = [];
var Unique_Names = [];
var IndexString='';
for(var i=0;i<Ids.length;i++){
    if(IndexString.indexOf('|'+Ids[i]+'|')==-1){
        Unique_Ids.push(Ids[i]);
        Unique_Names.push(Names[i]);
        IndexString+='|'+Ids[i]+'|'
    }
}

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386560

A solution with an temporary object for the ids and a for loop.

function unique(o) {
    var obj = {},
        r = { ids: [], names: [] },
        i;
    for (i = 0; i < o.ids.length; i++) {
        if (!obj[o.ids[i]]) {
            r.ids.push(o.ids[i]);
            r.names.push(o.names[i]);
            obj[o.ids[i]] = true;
        }
    }
    return r;
}

var Ids = ['123', '456', '789', '789'],
    Names = ['jack', 'jack', 'peter', 'peter'],
    result = unique({ ids: Ids, names: Names });

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

Upvotes: 1

Jaromanda X
Jaromanda X

Reputation: 1

Here's a tedious way of doing it

var Ids   = ['123',  '456',  '789',   '789'];
var Names = ['jack', 'jack', 'peter', 'peter'];

var i;
var t = {};
var unique_ids = [];
var unique_names = [];

for(i = 0; i < Ids.length; i += 1) {
    var id = Ids[i];
    var name = Names[i];
    t[id] = t[id] || name;
}
for (i in t) {
    if (t.hasOwnProperty(i)) {
        unique_ids.push(i);
        unique_names.push(t[i]);
    }
}

Upvotes: 1

millerbr
millerbr

Reputation: 2961

Not the most elegant solution, but this will fit your needs:

var Ids   = ['123',  '456',  '789',   '789'];
var Names = ['jack', 'jack', 'peter', 'peter'];
var Unique_Ids = [];
var Unique_Names = [];
for(i=0; i<Ids.length; i++) { Unique_Names.push(Names[i]); Unique_Ids.push(Ids[i]); } //Create 2 new arrays without polluting the old ones
var idToCompare;
for(id=0; id<Unique_Ids.length; id++) {
    idToCompare = Unique_Ids[id];
    for(id2=id+1; id2<Unique_Ids.length; id2++) {
        if(Unique_Ids[id] == Unique_Ids[id2]) {
            Unique_Ids.splice(id2, 1);
            Unique_Names.splice(id2, 1);
            id2--;
        }
    }
}

First it creates a copy of each of your arrays. This will go through each element of your array Unique_Ids, checking if a duplicate and then if so, removing it from both Unique_Ids and Unique_Names. This will probably be quite slow if done on very large arrays.

As asked for in comments, here is a solution that will change the original arrays:

var Ids   = ['123',  '456',  '789',   '789'];
var Names = ['jack', 'jack', 'peter', 'peter'];
var idToCompare;
for(id=0; id<Ids.length; id++) {
    idToCompare = Ids[id];
    for(id2=id+1; id2<Ids.length; id2++) {
        if(Ids[id] == Ids[id2]) {
            Ids.splice(id2, 1);
            Names.splice(id2, 1);
            id2--;
        }
    }
}  

Upvotes: 1

Salah Nour ElDin
Salah Nour ElDin

Reputation: 508

use this little code :

 var Ids   = ['123',  '456',  '789',   '789'];
var Names = ['jack', 'jack', 'peter', 'peter'];
var uniqueIds = [];
var uniqueNames = [];
var i=0;
$.each(Ids, function(i, el){
    if($.inArray(el, uniqueIds) === -1) {
        uniqueIds.push(el);
        uniqueNames.push(Names[i]);
        console.log(Names[i]);
        i++;

        }
});

Upvotes: 1

Related Questions