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, either array.filter
or array.reduce
and a temporary key-value store that records names you've already encountered.
const names = [{
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'
}
]
const fn = {};
const ln = {};
const uniqueNames = names.filter(name => {
// Check if it already exists.
const wasVisited = Boolean(fn[name.firstN]) || Boolean(ln[name.lastN]);
// Record the visit:
fn[name.firstN] = ln[name.lastN] = true;
// return the verdict
return !wasVisited;
});
console.log(uniqueNames)
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.
var names = [{
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'
}
]
function getUnique(data) {
var foundNames = {firstNs:Object.create(null),lastNs:Object.create(null)};
return data.reduce(function(result, obj) {
if (!(obj.firstN in foundNames.firstNs || obj.lastN in foundNames.lastNs)) {
foundNames.firstNs[obj.firstN] = true;
foundNames.lastNs[obj.lastN] = true;
result.push(obj);
}
return result;
}, []);
}
console.log(getUnique(names))
If you want to filter out any duplicate for any value that has been seen so far, then the following does that:
var names = [{
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'
}
]
function getUnique(data) {
var foundNames = {firstNs:Object.create(null),lastNs:Object.create(null)};
return data.reduce(function(result, obj) {
if (!(obj.firstN in foundNames.firstNs || obj.lastN in foundNames.lastNs)) {
result.push(obj);
}
foundNames.firstNs[obj.firstN] = true;
foundNames.lastNs[obj.lastN] = true;
return result;
}, []);
}
console.log(getUnique(names))
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:
Tom Jones, Tom Smith, Mary Smith
only Tom Jones will be returned, but if the order is:
Tom Jones, Mary Smith, Tom Smith
then both Tom Jones and Mary Smith will be returned.