I'm trying to do a neat little function for filtering an array of objects so that only object with unique value of a certain property is returned. This function works fine when prop
is just a plain property such as "email"
. But the function does not work when giving nested properties as argument, like "customer.email"
var unique = function (arr, prop) {
var found = [];
return arr.filter(function (obj) {
if (found.indexOf(obj[prop]) < 0) return found.push(obj[prop]);
});
};
reminders = unique(reminders, 'customer.email');
Is there a elegant way to give nested properties as argument, or is this something that should be avoided?