2017-04-13 68 views
0
 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 
] 

所以我想找到独特的名字寻找独特的属性,使名称仅出现一次在数组对象

所以从数据样本上面我的答案应该是戴夫·迈克和监测网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

你的逻辑看起来不一致。戴夫迈克保留,但是是戴夫便士的副本,并且汤姆便士和Meagher便士似乎被拒绝作为戴夫便士的副本,其中没有一个是最终结果。当然,“重复”测试只应该添加到结果中的成员?即汤姆便士应该保留。 – RobG

回答

1

array.map是错误的方法,因为它是1对1的数组转换。你正在寻找的是一种方法,可以减少数组,array.filterarray.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

你似乎想建立一个结果,其中第一个名字和姓氏都是唯一的。我会用分别为名字和姓氏的键构建一个对象,然后测试两者。

请注意,这个逻辑与你的有些不同,因为某些原因你拒绝了'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))

请注意,在这两种情况下,r esult将是不稳定的,因为它依赖于将名称提供给源数据数组的顺序,例如,在顺序给出的名字:

Tom Jones, Tom Smith, Mary Smith 

只有汤姆·琼斯将被退回,但如果顺序是:

Tom Jones, Mary Smith, Tom Smith 

那么这两个汤姆·琼斯和玛丽·史密斯将被退回。

+0

由于前一个值的姓氏相同,因此我拒绝了“Tom Pence”。感谢您的解决方案。 –

+0

我在这里排除了订单。只有我将我的姓氏和名字与列表 –

+0

@ early_ninja-fine中的前一个姓名进行比较,第二个功能可以做到这一点。订单的东西只是让你意识到,最终它是你的选择。 :-) – RobG