当元素重复时,Javascript从多维数组中删除行
我有以下数组当元素重复时,Javascript从多维数组中删除行,javascript,multidimensional-array,Javascript,Multidimensional Array,我有以下数组 [{ 'firstname': 'John', 'surname': 'Smith', 'HouseNo': 'firtree farm' }, { 'firstname': 'Paul', 'surname': 'Smith', 'HouseNo': 'firtree farm' }, { 'firstname': 'John', 'surname': 'Smith', 'HouseNo': 'firtreefarm' }, { 'firs
[{
'firstname': 'John',
'surname': 'Smith',
'HouseNo': 'firtree farm'
}, {
'firstname': 'Paul',
'surname': 'Smith',
'HouseNo': 'firtree farm'
}, {
'firstname': 'John',
'surname': 'Smith',
'HouseNo': 'firtreefarm'
}, {
'firstname': 'John',
'surname': 'Smith',
'HouseNo': 'firtree farmhouse'
}, {
'firstname': 'Paul',
'surname': 'Smith',
'HouseNo': 'firtree farmhouse'
}, {
'firstname': 'Paul',
'surname': 'Smith',
'HouseNo': 'FirTree farmhouse'
}]
我需要生成另一个数组,该数组不包含元素“HouseNo”的副本,只包含元素“HouseNo”。它还需要不区分大小写
即
该应用程序是基于邮政编码搜索返回的一组地址。然后,我可以提供一个他们可以选择的独特houseNo的过滤列表
MrWarby您可以跟踪对象中已看到的项目,如果您得到的项目不在已看到的
中,则将其推送到结果中
var seen = {};
var result = data.reduce(function(result, current) {
if (!seen[current.HouseNo]) {
seen[current.HouseNo] = true;
result.push(current.HouseNo);
}
return result;
}, []);
console.log(result);
输出
[ 'firtree farm',
'firtreefarm',
'firtree farmhouse',
'FirTree farmhouse' ]
如果您想保持对象结构,那么在推到结果时,只需创建如下对象
result.push({HouseNo: current.HouseNo});
var result = data.reduce(function(result, current) {
result[current.HouseNo] = true;
return result;
}, {});
console.log(Object.keys(result));
结果是
[ { HouseNo: 'firtree farm' },
{ HouseNo: 'firtreefarm' },
{ HouseNo: 'firtree farmhouse' },
{ HouseNo: 'FirTree farmhouse' } ]
如果数据的顺序无关紧要,那么您只需继续将HouseNo
添加到对象中,最后像这样获得键
result.push({HouseNo: current.HouseNo});
var result = data.reduce(function(result, current) {
result[current.HouseNo] = true;
return result;
}, {});
console.log(Object.keys(result));