6
Object1 = {connectorIndex: 1, nodeID: 6, Connectors: Object}
Object2 = {connectorIndex: 1, nodeID: 6, Connectors: Object}
Connector: {name: "ALAND", key: "", description: "Departure country (country from which the goods are sent)"}

There are two objects in same array. The connector objects are identical. How do I remove duplicate elements and get the final array with one object?

var array = [object 1, object 2];

object 2 is the duplicate to remove from the array.

1
  • There are no duplicate objects, two objects are never the same, so comparing objects is tricky, if there is some other way you can solve this, it's probably better, otherwise you'd have to check each property by recursively iterating to also compare the referenced Object etc. Commented Nov 15, 2014 at 6:35

5 Answers 5

15

this would do it if you are looking for exact matches:

function remove_duplicates(objectsArray) {
    var usedObjects = {};

    for (var i=objectsArray.length - 1;i>=0;i--) {
        var so = JSON.stringify(objectsArray[i]);

        if (usedObjects[so]) {
            objectsArray.splice(i, 1);

        } else {
            usedObjects[so] = true;          
        }
    }

    return objectsArray;

}

var objectsArray = [{a:'foo',b:'bar'}, {a:'foo',b:'bar'}];
var clean = remove_duplicates(objectsArray);
Sign up to request clarification or add additional context in comments.

5 Comments

worked but if i have 3 similar object its not working #Jonathan Crowe
What are the objects? This code will only remove exact object matches. Maybe you are looking to remove any matching data even if there are partial matches?
Object1 = {connectorIndex: 1, nodeID: 6, Connectors: Object} Object2 = {connectorIndex: 1, nodeID: 6, Connectors: Object}Object3 = {connectorIndex: 1, nodeID: 6, Connectors: Object} Object4 = {connectorIndex: 1, nodeID: 6, Connectors: Object}
What is Object? If this script is not removing it there is probably some difference in the Object values. Are you looking for a script that only looks at the top level keys for comparison?
Fantastic stuff mate
3

The challenge with using JSON.stringify is that if the object might contain a circular reference, then it will throw an exception, e.g.

var x1 = {};
x1.a = x1;
JSON.stringify(x1); // <- Uncaught TypeError: Converting circular structure to JSON

As alluded to however, if you are comparing objects and not values, then you can't just do an equality comparison, as this will always be false for different objects (even if they have the same properties with the same values).

If you were simply comparing values, then something like the below would work

var x = [1,2,2,3,4,2,6];  // Source list
var x2 = [];              // Will hold the de-duped result
x.forEach(function(elem){if(x2.indexOf(elem) === -1) x2.push(elem);});
x2;                       // <- [1, 2, 3, 4, 6]

If you want to compare object properties to one level you could write something like the below (there may be an easier way - just whipped this together)

function sameValues(o1, o2){
    for(p in o1){
        if(o1[p] !== o2[p]) return false;
    }
    for(p in o2){
        if(o2[p] !== o1[p]) return false;
    }
    return true;
}

var Object1 = {connectorIndex: 1, nodeID: 6, Connectors: Object};
var Object2 = {connectorIndex: 1, nodeID: 6, Connectors: Object};
var Object3 = {connectorIndex: 1, nodeID: 7, Connectors: Object};
var Object4 = {connectorIndex: 2, nodeID: 7, Connectors: Object};
var Object5 = {connectorIndex: 1, nodeID: 7, Connectors: Object};
var arr = [Object1, Object2, Object3, Object4, Object5];

var result = [];
arr.forEach(function(e1){
    if(!result.some(function(e2){return sameValues(e1,e2);})){
        // No existing object with same top level values in target array, so add
        result.push(e1);
    }
});
// result will have the 1st, 3rd, and 4th object only

2 Comments

it is working only when obj1 obj2 are in order...if obj1 obj4 obj3 if in this it is not working
It's working for me. Sure you copy/pasted right? If I do the above code but with the array as "var arr = [Object1, Object4, Object3, Object2, Object5];" then I see Object1, Object4, and Object3 in the output, and the others removed - which is what you'd expect as Object2 and Object 5 are duplicates.
1

This is how I prefer to remove duplicate objects from javascript array-

var fieldArray = [{ "name": "tom", "text": "tasty" },{ "name": "ram", "text": "rty" },{ "name": "ram", "text": "rty" },{ "name": "shyam", "text": "tasty" },{"name": "ram", "text": "rty" },{ "name": "tom", "text": "tasty" }];
fieldArray = fieldArray.reduce(function(field, e1){  
	var matches = field.filter(function(e2){return e1.name== e2.name}); 
	if (matches.length == 0){ 
		field.push(e1);  
	}return field;
}, []);
alert(JSON.stringify(fieldArray));

This works perfect for me.

Comments

0

Use an array to hold which items you already have and use filter()

var keys = []
yourArray.filter(function(v) {
  if (keys.indexOf(v.whatIAmLookingFor) < 0) {
    keys.push(v.whatIAmLookingFor)
    return true
  }
  return false
})

Wrap it in a function and call it a day. Pass in a function for the test, and call it an even better day.

Comments

0

Not sure how performant this is, but I kinda like it for it's succinctness, works for me (its ES6, but Im sure you could figure out the plain ol' ES5):

let dedupe = (arr, keys) => {
  let deduped = [];
  arr.forEach(i => {
    if (deduped.length === 0) {
      deduped.push(i);
    } else {
      let exists = deduped.find(x => {
        return keys.every(key => {
          return i[key] === x[key];
        });
      });
      if (!exists) {
        deduped.push(i);
      }
    }
  });
  return deduped;
};

objArr = dedupe(objArr, ['key', 'test', 'foo']);

Comments

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.