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 ;) – Tomas Ramirez 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

Tomas Ramirez Sarduy
  • 17,294
  • 8
  • 69
  • 85