Search code examples
javascriptarraysunderscore.jsarray-filter

How to remove duplicate data values from an array of objects in javascript?


I want to be able to use array methods or some other form of javascript processing without any external javascript library in order to iterate through a multi dimensional array and remove any duplicate values. Here is an example below:

var places = [{
    "category": "other",
    "title": "harry University",
    "value": 4788,
    "id": "1"
  },
  {
    "category": "traveling",
    "title": "tommy University",
    "value": 5460,
    "id": "2"
  },
  {
    "category": "education",
    "title": "jerry University",
    "value": 7853456,
    "id": "3"
  },
  {
    "category": "business",
    "title": "Charlie University",
    "value": 6779589,
    "id": "4"
  },
  {
    "category": "business",
    "title": "NYU",
    "value": 0117824,
    "id": "5"
  }
];

I have tried to use array.filter to remove the duplicate category values but the approach I have taken does not work.

Here is my code

places = places.map(JSON.stringify).reverse()
  .filter(function(item, index, places) {
    return places.indexOf(item, index + 1) === -1;
  })
  .reverse().map(JSON.parse)
console.log(places)

The above doesn't work because it is operating on the whole array so the data does not change.

When I use a library like underscore.js they have a method that works

let newPlaces = [];
_.each(_.uniq(_.pluck(places, "category")), function(category) {
  newPlaces.push(_.findWhere(places, {
    category: category
  }));
});
console.log(newPlaces);

But I want to be able to use .filter or some other vanilla javascript method.


Solution

  • Your example is not a multidimensional array. But simply a regular array of objects. A multi-dimensional array would be something like let a = [[1],[1]]; which is an array of arrays. Anyway, are you looking to simply remove all objects(items in the array) for which a category has already appeared once. You can do this with the Vanilla Javascript filter method like you wanted. You simply have to use an object to keep count of whether the object has appeared before or not. Here is a simple approach

    function removeDuplicates(arrToBeFiltered, keyToMatch){
      let dups = {};
      return arrToBeFiltered.filter(function(item){ 
        if(item[keyToMatch] in dups){return false;} 
        else {
            dups[item[keyToMatch]] = true; return true;
        }}); 
    }
    places = removeDuplicates(places,'category');