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.