Search code examples
javascriptarraysduplicatesunique

How to remove duplicated OBJECTS from JavaScript array?


What's the best way to remove duplicate objects from array of objects?

From

var arr = 
    [
        {"name":"Joe", "age":17}, 
        {"name":"Bob", "age":17}, 
        {"name":"Carl", "age": 35},
        {"name":"Bob", "age":35},
        {"name":"Joe", "age":17}, 
    ]

when duplicates removed, the expected result is

res= arr = 
    [
        {"name":"Joe", "age":17}, 
        {"name":"Bob", "age":17}, 
        {"name":"Carl", "age": 35},
        {"name":"Bob", "age":35},
    ]

(5 objects, 1 duplicate, 4 left).

The number of properties of each object is fixed, the properties names are the same for each array. However, from array to array they may not be just "name" and "age" as above, but the names of the properties could be any.

@Pointy Please treat the duplicate word in the question above as 'duplicate' in the verbal sense - the object with the same number of properties, the same properties and the same values of that properties respectively.

THIS IS NOT DUPLICATE OF Remove Duplicates from JavaScript Array


Solution

  • You could use an object for lookup, if an object is alreday inserted or not.

    Edit:

    Update for getting all properties of the object and use the values for the key. If only some properties should be used for it, then I suggest to use an array with the relavant keys, like

    ['name', 'age']
    

    and use it with

    var key = ['name', 'age'].map(function (k) { return a[k]; }).join('|');
    

    var arr = [{ "name": "Joe", "age": 17 }, { "name": "Bob", "age": 17 }, { "name": "Carl", "age": 35 }, { "name": "Bob", "age": 35 }, { "name": "Joe", "age": 17 }],
        filtered = arr.filter(function (a) {
            var key = Object.keys(a).map(function (k) { return a[k]; }).join('|');
            if (!this[key]) {
                return this[key] = true;
            }
        }, Object.create(null));
    
    console.log(filtered);