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
]
所以我想找到唯一的名称,以便名称只出现一次
所以我从上面的数据样本中回答的应该是Dave Mike和Surv Dyal
为此我只得到了这一点
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);
}
答案 0 :(得分:1)
array.map
是错误的使用方法,因为它是一对一的数组转换。您正在寻找的是一种可以减少数组的方法,array.filter
或array.reduce
以及记录您已经遇到的名称的临时键值存储。
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)
答案 1 :(得分:1)
您似乎想要构建一个名字和姓氏都是唯一的结果。我分别用名字和姓氏的密钥构建一个对象,然后对它们进行测试。
请注意,这种逻辑与您的逻辑略有不同,出于某种原因,您拒绝了Tom Pence'即使根据上述逻辑,名称也应该在结果中。
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))

如果您要为目前为止看到的任何值过滤掉任何重复项,那么以下内容就是:
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))

请注意,在这两种情况下,结果都将不稳定,因为它依赖于提供给源数据数组的名称的顺序,例如,按顺序给出姓名:
Tom Jones, Tom Smith, Mary Smith
只会退回汤姆琼斯,但如果订单是:
Tom Jones, Mary Smith, Tom Smith
然后汤姆琼斯和玛丽史密斯都将被退回。