繁体   English   中英

当元素重复时,Javascript从多维数组中删除行

[英]Javascript remove row from multidimensional array when elements are duplicated

我有以下数组。

[{
  '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': 'firtree farm'
}, {
  'HouseNo': 'firtreefarm'
}, {
  'HouseNo': 'firtree farmhouse'
}, {
  'HouseNo': 'FirTree farmhouse'
}]

该应用程序是根据邮政编码搜索返回的地址集。 然后,我可以提供唯一房屋编号的过滤列表,他们可以选择表格。

沃比先生

您可以跟踪已经看到项目中的对象,如果你是不是有在一个项目seen ,然后将其推到result

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});

结果将是

[ { HouseNo: 'firtree farm' },
  { HouseNo: 'firtreefarm' },
  { HouseNo: 'firtree farmhouse' },
  { HouseNo: 'FirTree farmhouse' } ]

如果数据的顺序无关紧要,那么您可以简单地继续将HouseNo添加到对象中,最后获得像这样的keys

var result = data.reduce(function(result, current) {
    result[current.HouseNo] = true;
    return result;
}, {});
console.log(Object.keys(result));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM