0

All:

I wonder if there is a way to quickly remove duplicated object from array.

The object inside the array is positon info, like:

var coordinates = [
{x:1, y:2},
{x:1, y:3},
{x:1, y:2},
{x:1, y:2}
]

The final output should be only 2 points:

[
{x:1, y:2},
{x:1, y:3},
]

The only way I can think out is:

var uniquetable = {}
coordinates.forEach(function(d, i){
    uniquetable[d.x+"_"+d.y] = d;
});
coordinates  = [];
for(var k in uniquetable) {
    coordinates.push( uniquetable[k] );    
}

But when the position is multi-dimension(like adding extra object-type attributes, I have no idea how to do like this), for example:

var coordinates = [
{x:1, y:2, style:{color:'red'}},
{x:1, y:3, style:{color:'blue'}},
{x:1, y:2, style:{color:'green'}},
{x:1, y:2, style:{color:'red'}}
]

I wonder how can I quickly remove duplicated objects?

Kuan
  • 11,149
  • 23
  • 93
  • 201
  • `JSON.stringify()` usually works to produce a comparable version of a data object. if your keys are not in a consistent order, its going to be more complicated. much more. – dandavis Jun 23 '15 at 22:06
  • 2
    You probably want to check for deep equality. Various libraries implement such a function. Basically recursively iterate over the data structures and compare. – Felix Kling Jun 23 '15 at 22:07
  • @FelixKling Could you name some libs which can do that? – Kuan Jun 23 '15 at 22:19
  • 1
    https://lodash.com/, http://underscorejs.org/, https://www.google.com/search?q=deep+equal+javascript – Felix Kling Jun 23 '15 at 22:20
  • @FelixKling underscore.js is the first think out of my mind, but I can not figure out how to use its _.uniq function with array with objects, could you give a simple example? – Kuan Jun 23 '15 at 22:22
  • `_.uniq` doesn't allow you to provide your own comparison algorithm unfortunately. There basically two ways: Either use deep equality comparison and iterate yourself, resulting in an `O(n^2)` algorithm (can probably be optimized), or compute a hash for each value, in which case you can use `_.uniq`. – Felix Kling Jun 23 '15 at 22:23
  • Related: [Find Duplicates in array](http://stackoverflow.com/q/7055508/572670). This thread shows some approaches to find the dupes, and also discusses a lower bound for this problem. This is not a big step from finding dupes to removing them. – amit Jun 23 '15 at 22:26
  • @amit thanks, I may need time to study that. – Kuan Jun 23 '15 at 22:30

1 Answers1

2

You can use filter. You check with uniqueFn if the string exists in the temp var. Then you keep the element if it is true

var coordinates = [
  {x:1, y:2, style: {color:'red'}},
  {x:1, y:3, style: {color:'blue'}},
  {x:1, y:2, style: {color:'green'}},
  {x:1, y:2, style: {color:'red'}}
];

var uniqueFn = function(val){
  return [val.x, val.y, val.style.color].join()
}, temp = [];

coordinates = coordinates.filter(function(val){
  return temp.indexOf(uniqueFn(val)) == -1 ? temp.push(uniqueFn(val)) : false
});

document.write("<pre>" + JSON.stringify(coordinates, 0, 3) + "</pre>")