The problem with using JSON.stringify is that if the object can contain a circular reference, then it throws an exception, for example.
var x1 = {}; x1.a = x1; JSON.stringify(x1);
As mentioned, however, if you are comparing objects, not values, then you cannot 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 just comparing the values, something like below would work
var x = [1,2,2,3,4,2,6];
If you want to compare the properties of an object with one level, you can write something like below (maybe an easier way is to just whip it 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);})){
source share