var name = [ {
firstN: 'Dave',
lastN: 'Mike',
fullName: 'Dave Mike
},
{
firstN: 'Dave',
lastN: 'Pence',
fullName: 'Dave Pence'
},
{
firstN: 'Tom',
lastN: 'Pence',
fullName: 'Tom Pence'
}, {
firstN: 'Meagher',
lastN: 'Pence',
fullName: 'Meagher Pence'
}, {
firstN: 'Surv',
lastN: 'dyal',
fullName: 'Surv dyal
}
................and so on like 100s
]
So I want to find unique names so that names occur only once
So my answer from above data sample should be Dave Mike and Surv Dyal
For this I have gotten only this far
var list =Object.keys(newList).map( function(key){
var temp_firstName = newList[key].firstName + ','+ key;
var temp_lastName = newList[key].lastName + ','+ key;
console.log( temp_lastName,temp_firstName );
return temp_lastName, temp_firstName;
});
console.log(list);
//console.log(newList);
}
array.map
is the wrong method to use since it's a 1-to-1 array transformation. What you're looking for is a method that can trim down an array, eitherarray.filter
orarray.reduce
and a temporary key-value store that records names you've already encountered.You seem to want to build a result where the first name and last name are both unique. I'd build an object with keys for first names and last names separately, then test both.
Note that this logic is a little different to yours, where for some reason you've rejected 'Tom Pence' even though according to the above logic the name should be in the result.
E.g.
If you want to filter out any duplicate for any value that has been seen so far, then the following does that:
Note that in both cases, the result will be unstable as it relies on the order that names are supplied to the source data array, e.g. given names in the order:
only Tom Jones will be returned, but if the order is:
then both Tom Jones and Mary Smith will be returned.