How to remove duplicate object values โ€‹โ€‹from JScript / Javascript List array

I have an arrayList with objects in it. I only need to show one value if the objects have the same values.

Example: [{a: 0, b: 1}, {a: 1, b: 0}, {a: 0, b: 1}] In the above example, I only need to show the first and second objects and skip the third object, because it is the same as the first object.

Note: the objects in the array can be infinite, I cannot specify the index value. Can anyone help me get a general solution.

Here's what I've tried:

points = [];
newarr = [];
locArray = [ {a:0,b:1},{a:1,b:0},{a:0,b:1} ];

if(abc!=null){
removeDuplicateCoordinates();
                        $.each(newarr,function(key,val){
                           points.push([val.a,val.b]);

                        });

}

function removeDuplicateCoordinates(){
                var arr = locArray;
                $.each(arr, function(index,item){
                    if(searchForItem(newarr,item)<0){
                        newarr.push(item);
                    }
                });
            }
            function searchForItem(array, item){
                var i, j, current;
                for(i = 0; i < array.length; ++i){
                    if(item.length === array[i].length){
                        current = array[i];
                        for(j = 0; j < item.length && item[j] === current[j]; ++j);
                        if(j === item.length)
                            return i;
                    }
                }
                return -1;
            }

      

+3


source to share


3 answers


First of all Array.indexOf () will not compare objects.

We know that :

JavaScript objects use a reference type. Two different objects are never equal, even if they have the same properties. Only comparing the same object reference with itself gives true.



So, the easiest and fastest way, IMHO , would be to compare yourself. Here is a working JSFiddle

var locArray = [{ a: 0, b: 1 }, { a: 1, b: 0 }, { a: 0, b: 1 }];

//We will try to find if point alrady exists in array
Array.prototype.indexOfPoint = function(point) {
    for (var i = 0; i < this.length; i++) {
        var arrPoint = this[i];
        if (arrPoint.a === point.a && arrPoint.b === point.b)
            return i;
    }
    return -1;
};

Array.prototype.uniquePoints = function() {
    var a = [];
    for (var i = 0; i < this.length; i++) {
        var currentPoint = this[i];
        if (a.indexOfPoint(currentPoint) < 0) {
            a.push(currentPoint);
        }
    }
    return a;
};

var newarr = locArray.uniquePoints();
console.log(newarr);

      

+1


source


Screenshot demo try this one



    newarr = [];
    testarr = [];
    locArray = [ {a:0,b:1},{a:1,b:0},{a:0,b:1} ];
    for (var i = 0; i<locArray.length;i++)
        {
    var idx = $.inArray(String(locArray[i].a)+String(locArray[i].b), testarr);
    if (idx == -1) {
      testarr.push(String(locArray[i].a)+String(locArray[i].b));
      newarr.push(locArray[i]);
        }
     }
    console.log(newarr);

      

+3


source


One of my favorite methods:

usedArray = {};
locArray = [ {a:0,b:1},{a:1,b:0},{a:0,b:1} ];
for (key in locArray) {
    if (usedArray[JSON.stringify(locArray[key])] == undefined) {
        console.log(JSON.stringify(locArray[key]));
        usedArray[JSON.stringify(locArray[key])] = true;
    }
}

      

Don't know anything about how fast, but it works for me every time. The fiddle works .

instead, console.log(JSON.stringify(locArray[key]));

you can fill in a new array:

newarr.push(locArray[key]);

      

EDIT

The test width of 100000 objects in a fiddle is ~ 300ms, I can live with that.

+2


source







All Articles