132
rated 0 times
[
134]
[
2]
/ answers: 1 / hits: 92390
/ 8 Years ago, thu, october 6, 2016, 12:00:00
I'm learning JS. Supposing I have the below array of objects:
var family = [
{
name: Mike,
age: 10
},
{
name: Matt
age: 13
},
{
name: Nancy,
age: 15
},
{
name: Adam,
age: 22
},
{
name: Jenny,
age: 85
},
{
name: Nancy,
age: 2
},
{
name: Carl,
age: 40
}
];
Notice that Nancy is showing up twice (changing only the age). Supposing I want to output only unique names. How do I output the above array of objects, without duplicates? ES6 answers more than welcome.
Related (couldn't find a good way for usage on objects):
EDIT Here's what I tried. It works well with strings but I can't figure how to make it work with objects:
family.reduce((a, b) => {
if (a.indexOf(b) < 0 ) {
a.push(b);
}
return a;
},[]);
More From » arrays