从对象数组中获取唯一的属性对(Angular 2+)

时间:2019-01-24 00:21:08

标签: arrays angular object unique

我正在尝试从Angular 7中的对象数组中获取唯一的名+姓氏组合的数组。

下面是示例数据:

[{"First_Name": "Jane", "Last_Name": "Doe", "State": "Louisiana"},
{"First_Name": "John", "Last_Name": "Doe", "State": "Louisiana"},
{"First_Name": "Sally", "Last_Name": "Smith", "State": "Iowa"},
{"First_Name": "Sally", "Last_Name": "Smith", "State": "Pennsylvania"},
{"First_Name": "Jack", "Last_Name": "Welch", "State": "New York"}]

理想情况下,它将返回此内容(删除“状态”并删除“萨莉·史密斯”的重复数据):

[{"First_Name": "Jane", "Last_Name": "Doe"},
{"First_Name": "John", "Last_Name": "Doe"},
{"First_Name": "Sally", "Last_Name": "Smith"},
{"First_Name": "Jack", "Last_Name": "Welch"}]

下面是我的代码。它返回唯一的姓,但不检索名字。

getUniqueNames(list) {
    const valuelist = list.map(list => list['First_Name'] && list['Last_Name']);
    this.uniquevalues = valuelist.filter((x, i, a) => x && a.indexOf(x) === i);
    this.uniquevalues.sort(function(a, b){
        if(a < b) { return -1; }
        if(a > b) { return 1; }
        return 0;
    });
    return this.uniquevalues;
  }

如何最好地获得唯一的名字+姓氏列表?

0 个答案:

没有答案