1

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.

9
  • You mention you don't want to use indexOf, but you're already doing it? return Names.indexOf(item) Commented Feb 21, 2016 at 14:44
  • if you have array.filter, you have array.indexOf Commented Feb 21, 2016 at 14:44
  • @JaromandaX Well I'm trying to find another approach .. Commented Feb 21, 2016 at 14:45
  • also array.indexOf works in IE9+ - do you really need to support IE8 or earlier? Commented Feb 21, 2016 at 14:45
  • @JaromandaX Yeah .. the most of my website's user use IE7 :-) Commented Feb 21, 2016 at 14:46

5 Answers 5

1

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>');

Sign up to request clarification or add additional context in comments.

Comments

1

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++;

        }
});

Comments

1

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--;
        }
    }
}  

3 Comments

he wants new arrays for the output
I have updated the answer to include the creation of 2 new arrays
Good .. but please add previous solution again to your solution .. I mean please keep both of them ..
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]);
    }
}

6 Comments

in is in in for ... in - docs ... don't worry, IE7 will handle it, it's been in javascript since June 1997
Look, this solution is very good .. just for my information, may you please show me how can I do that without new arrays? I mean applying changes on Ids and Names?
use the solution that does what you didn't ask in the question, it does exactly what you asked in your comment
The algorithm of this solution seems correct to me (using an object) .. but I don't know why it doesn't work jsfiddle.net/oem9r8kn
because, if you look in your browsers developer tools console, you'll see I made a typo ... I had Name[i] instead of Names[i]
|
0

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]+'|'
    }
}

3 Comments

Thank you .. But I mentioned: I don't want a solution containing this indexOf() method
In case you don't find a better answer,here is an easy fix: coding-journal.com/array-indexof-in-internet-explorer-9
I also edited to use indexOf on a string instead of an array, which is supported on explorer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.