【问题标题】:Angular - Remove duplicates in array with same name elementAngular - 删除具有相同名称元素的数组中的重复项
【发布时间】:2020-01-01 10:31:12
【问题描述】:

问题很简单。

我怎样才能用“alarma”元素过滤这个数组来只得到一个

0: {alarma: 1797, nroSerie: "8764368", nombre: "Alarma cable cortado", fechaInicio: 1566194460, fechaFin: 1566311460}
1: {alarma: 1797, nroSerie: "8764368", nombre: "Alarma cable cortado", fechaInicio: 1566311460, fechaFin: 1566311580}
2: {alarma: 1797, nroSerie: "8764368", nombre: "Alarma cable cortado", fechaInicio: 1566311580, fechaFin: null}

【问题讨论】:

  • array.filter()
  • array.filter 将返回所有具有指定条件的元素,它是相反的。您可以使用 Map,'alarma' 值作为键,整个元素作为值
  • 我尝试过这样的事情,但并不完全是我需要的:array.filter((el, i, a) => i === a.indexOf(el));跨度>

标签: angular


【解决方案1】:

你可以使用库中的lodash和函数uniqBy,这将是最快的方法:)

https://lodash.com/docs/4.17.15#uniqBy

【讨论】:

    【解决方案2】:

    您可以创建一个通用函数GetDistinctValues(Source, Filterkey),它将获取任何数据数组和数组中对象的任何过滤键

    试试这样:

      dataarray = [
        { alarma: 1797, nroSerie: "8764368", nombre: "Alarma cable cortado", fechaInicio: 1566194460, fechaFin: 1566311460 },
        { alarma: 1797, nroSerie: "8764368", nombre: "Alarma cable cortado", fechaInicio: 1566311460, fechaFin: 1566311580 },
        { alarma: 1797, nroSerie: "8764368", nombre: "Alarma cable cortado", fechaInicio: 1566311580, fechaFin: null }
      ]
    
      ngOnInit() {
        var distinctValue = this.GetDistinctValues(this.dataarray, 'alarma')
        console.log(distinctValue)
      }
    
    
      GetDistinctValues(Source: Array<any>, FilterKey: string = null): Array<any> {
        let DistinctArray = [];
        try {
          Source.forEach((e) => {
            if (FilterKey !== null && FilterKey !== undefined && FilterKey !== "") {
              if (DistinctArray.filter(((DE) => DE[FilterKey] === e[FilterKey])).length <= 0)
                DistinctArray.push(e);
            }
            else {
              if (DistinctArray.indexOf(e) === -1)
                DistinctArray.push(e);
            }
          });
        } catch (error) {
          DistinctArray = [];
        }
        return DistinctArray;
      }
    

    【讨论】:

      【解决方案3】:

      一种选择是使用

      array.indexOf(obj)

      功能。如果数组已经有相同的元素, indexOf 函数将返回一些有效的索引。在做之前

      array.push(obj)

      检查

      array.indexOf(obj).

      功能优先

      【讨论】:

        【解决方案4】:

        如果您想通过给定键删除重复项而不使用 lodash 库,您可以执行以下操作:

        对于给定数组[{a: 5, b: 7}, {a: 5, b: 99}, {a: 6, b: 1}, {a: 6, b: 0}],预期结果可以是[{a: 5, b: 99}, {a: 6, b: 0}](取最后一个值)

        简单地实现这个:

        1. 创建一个将键作为“a”属性值的对象:
        const array = [{a: 5, b: 7}, {a: 5, b: 99}, {a: 6, b: 1}, {a: 6, b: 0}];
        const unique = array.reduce((acc, value) => { 
          acc[value.a] = value; 
          return acc;
        }, {});
        

        unique 对象如下:

        {"5":{"a":5,"b":99},"6":{"a":6,"b":0}}
        
        1. 使用以下方法获取此对象的值:
        const result = Object.values(unique);
        

        result 值将是:

        [{"a":5,"b":99},{"a":6,"b":0}]
        

        如果您只想获取重复项的第一项,请将代码更改为

        const array = [{a: 5, b: 7}, {a: 5, b: 99}, {a: 6, b: 1}, {a: 6, b: 0}];
        const unique = array.reduce((acc, value) => { 
          acc[value.a] = acc[value.a] || value; // note the changes at this line
          return acc;
        }, {});
        const result = Object.values(unique);
        

        输出将是:

        [{"a":5,"b":7},{"a":6,"b":1}]
        

        【讨论】:

          猜你喜欢
          • 2014-01-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-18
          • 2020-07-15
          • 2019-09-02
          • 2023-01-02
          相关资源
          最近更新 更多