1

What the best way to remove duplicates from an array of objects?

var array = [
  {a: 0, b: 0, c: 0},
  {a: 0, b: 0, c: 0},

  {a: 1, b: 1, c: 1},
  {a: 1, b: 1, c: 1},

   //..... etc
];

And, i want to get like:

[
  {a: 0, b: 0, c: 0},
  {a: 1, b: 1, c: 1}
];

PS: the keys (a, b, c) have only primitive data type (String, Number)

Please, without underscore.js and other libs.

Robbin
  • 87
  • 1
  • 4
  • 1
    What happened to the `c`s? – Dancrumb Jan 31 '13 at 19:41
  • 1
    Also, does order matter? – Dancrumb Jan 31 '13 at 19:41
  • 1
    Finally... why not use underscore? – Dancrumb Jan 31 '13 at 19:42
  • 1
    What is your equality function? I.e. what does it mean for two objects to be duplicative of one another? Are you assuming no-one will mutate elements of this array? – Mike Samuel Jan 31 '13 at 19:42
  • You should checkout this question, it offers a good comparison method -> http://stackoverflow.com/questions/1068834/object-comparison-in-javascript – Justin Bicknell Jan 31 '13 at 19:43
  • @Dancrumb, the order does not matter – Robbin Jan 31 '13 at 19:43
  • @Robbin: great... what about the other two questions? – Dancrumb Jan 31 '13 at 20:25
  • 2
    @Dancrumb, I dont want to use the underscore.js in my project, because there're no any reasons for it – Robbin Jan 31 '13 at 22:50
  • 1
    @Robbin, while you're free to chose, there actually *is* a good reason to use underscore: you want to take an array of objects and remove duplicates. Tom's solution below is fine, if you don't mind O(n^2) performance and if your objects will never change and if there can never be an `undefined` or `null` element, or if... you know, any number of things you might not have though of. Using libraries helps because they are often much more rigorously developed and tested than you have time to do yourself. – Dancrumb Feb 01 '13 at 14:51
  • Underscore or lodash or others have precisely the methods that you're asking for, so they're a great choice... unless this is homework... in which case... *shrug* – Dancrumb Feb 01 '13 at 14:52
  • 1
    100% agree with @Dancrumb, this is just a simple way to do this for small data, also I didn't check for `undefined` or `null` values, but that's homework ;) – Tom Sarduy Feb 01 '13 at 19:49
  • 1
    Does this answer your question? [Remove duplicates from an array of objects in JavaScript](https://stackoverflow.com/questions/2218999/remove-duplicates-from-an-array-of-objects-in-javascript) – leonheess Mar 11 '21 at 18:14

1 Answers1

2

I'm sure there is better ways to do this, but you can use this prototype function.

Array.prototype.removeDuplicates = function () {
    var r = new Array();
    o:for(var i = 0, n = this.length; i < n; i++)
    {
        for(var x = 0, y = r.length; x < y; x++)
            if(r[x].a==this[i].a && r[x].b==this[i].b && r[x].c==this[i].c)
                continue o;
        r.push(this[i]);
    }
    return r;
}

How to use it

var arr = [
  {a: 0, b: 0, c: 0},
  {a: 0, b: 0, c: 0},
  {a: 1, b: 1, c: 1},
  {a: 1, b: 1, c: 1},

   //..... etc
];
var uniques = arr.removeDuplicates();
console.log(uniques);

Note:

You should avoid this for big arrays, out there are better solutions

Tom Sarduy
  • 16,878
  • 8
  • 67
  • 83
  • 1
    Could you rewrite your example without *o:* ? – Robbin Feb 01 '13 at 12:05
  • @Robbin: I think a better idea is that you rewrite the code. *Hint: use a bit (value 0 or 1) inside loop* – Tom Sarduy Feb 01 '13 at 20:13
  • How big of arrays should be avoided, and I have been looking, anyone have a link or can state a better solution, I have the same issue but have arrays with a couple thousand objects to go through... – Lion789 Oct 08 '13 at 19:56
  • @Lion789: In that case you need to study algorithms for that and maybe some heuristics – Tom Sarduy Oct 09 '13 at 00:38
  • Ok, by the way, using the above prototype, how would you go about removing duplicates and merging two or more arrays? – Lion789 Oct 10 '13 at 02:53