【问题标题】:How to remove duplicates in array by choosing which one to keep? [closed]如何通过选择保留哪一个来删除数组中的重复项? [关闭]
【发布时间】:2019-10-10 21:00:25
【问题描述】:

问题

我有一个 Javascript 数组,它可以包含重复项(只有 2 个),键值 color 具有相同的值。

  var array = [
    {
      "id": "1",
      "color": "red",
      "animal": null
    },
    {
      "id": "2",
      "color": "red",
      "animal": "cat"
    },
    {
      "id": "3",
      "color": "green",
      "animal": "dog"
    }
  ];

我想删除键 color 上的重复项,只保留键为 animal 而不是 null 的唯一对象。

  var uniqueArray = [
    {
      "id": "2",
      "color": "red",
      "animal": "cat"
    },
    {
      "id": "3",
      "color": "green",
      "animal": "dog"
    }
  ];

我尝试了什么

  var obj = {};

  for (var i = 0; i < array.length; i++) {
    obj[array[i]['name']] = array[i];
  }

  var uniqueArray = new Array();
  for (var key in obj) {
    uniqueArray.push(obj[key]);
  }

结果如下:

var uniqueArray = [
    {
      "id": "1",
      "color": "red",
      "animal": "null"
    },
    {
      "id": "3",
      "color": "green",
      "animal": "dog"
    }
  ];

我不知道如何选择要保留的对象。

注意:我不能使用 ES6

【问题讨论】:

  • 你的问题也没有很好的定义。如果有一对重复的颜色并且这两个都有一个非空动物怎么办?另外,如果某个颜色只有一个项目,但动物为空,最终数组中会包含还是排除呢?
  • 到目前为止你做了什么?

标签: javascript arrays object duplicates unique


【解决方案1】:

只需使用简单的filter 删除所有null 值:

var array = [{"id":"1","color":"red","animal":"cat"},{"id":"2","color":"red","animal":"null"},{"id":"3","color":"green","animal":"dog"},{"animal":"dog"}];
var uniqueArray = array.filter(function(e) {
  return e.animal != "null";
});
console.log(uniqueArray);

【讨论】:

    【解决方案2】:

    不使用 ES6

    var array = [{
        "id": "1",
        "color": "red",
        "animal": "cat"
      },
      {
        "id": "2",
        "color": "red",
        "animal": "null"
      },
      {
        "id": "3",
        "color": "green",
        "animal": "dog"
      }
    ];
    var a = [];
    var arr = [];
    array.forEach(function(e, i) {
      if (a.indexOf(e.color) == -1 && e.animal != "null") {
        a.push(e.color)
        arr.push(e)
      }
    })
    console.log(arr)

    【讨论】:

    • 字符串"null"与值null不同。
    【解决方案3】:

    我希望animal的值应该是null而不是'nul'。

    var array = [
        {
          "id": "1",
          "color": "red",
          "animal": null
        },
        {
          "id": "2",
          "color": "red",
          "animal": "cat"
        },
        {
          "id": "3",
          "color": "green",
          "animal": "dog"
        }
      ];
    

    如果您想根据颜色选择唯一值(在这种情况下为对象),最好创建另一个对象并将颜色作为获取唯一值的键。

    var obj = {};
    
    array.forEach(a => {
     if(!obj[a.color] && a.animal) {
        obj[a.color] = a;
     }
    });
    

    现在对象如下所示

    现在你得到一个包含基于颜色的唯一对象的对象,更重要的是你的动物属性不为空。 你说你想不出这里该选什么。 IMO 最好选择具有值而不是空值的值。

    注意:上面的代码为您提供了一个对象,但您可以通过以下操作轻松将其转换为数组。不完全确定 Object.values 是否是 es6 功能(在文档上没有找到任何内容)。

    var arr = Object.values(obj);
    

    如果您不喜欢上面的解决方案,您可以遍历对象并将其推送到数组中。

     var finalArr = [];
     Object.keys(obj).forEach(function(key) {
       finalArr.push(obj[key])
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-28
      • 2019-06-02
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 2021-11-12
      相关资源
      最近更新 更多