【问题标题】:How to remove duplicate property values in array如何删除数组中的重复属性值
【发布时间】:2020-12-06 17:51:50
【问题描述】:

我有一个包含 6 个元素的数组。每个元素都有 3 个属性:idvaluefriends

在我的代码中,我通过向某些元素的friends 属性添加新值来操作数组,但在某些情况下,这会导致friends 属性中的值重复。例如,在下面的myArray 中,元素 5 不能与元素 1 成为朋友两次——他们要么是朋友,要么不是朋友。

const myArray = [
    {"id": 1, "value": 75, "friends": 3},
    {"id": 2, "value": 40, "friends": 4},
    {"id": 3, "value": 60, "friends": 5},
    {"id": 4, "value": 62, "friends": [6, 1]},
    {"id": 5, "value": 55, "friends": [1, 1]},
    {"id": 6, "value": 33, "friends": [2, 1]}
];

如何删除myArrayfriends 属性中的重复值?

我想要的输出如下所示:

const myArray = [
    {"id": 1, "value": 75, "friends": 3},
    {"id": 2, "value": 40, "friends": 4},
    {"id": 3, "value": 60, "friends": 5},
    {"id": 4, "value": 62, "friends": [6, 1]},
    {"id": 5, "value": 55, "friends": 1},
    {"id": 6, "value": 33, "friends": [2, 1]}
];

我是 javascript 新手,不知道从哪里开始。我的直觉是首先获取所有friends,然后应用一些过滤功能:

const friendList = myArray.map(getFriends);
var uniqueFriends = [...new Set(friendList)];

但这显然行不通。

【问题讨论】:

  • 您的直觉很好,但更改并未就地完成。
  • 没有必要清理那个副作用,你可以使用solution来解决这个问题
  • 感谢@Yevgen Gorbunkov!刚刚注意到编辑并在该帖子中给出了答案。但我也会留下这篇文章,因为它将来可能会有所帮助。
  • @user72716 您需要循环遍历数组并根据需要更改friends 属性。请参阅下面的代码。

标签: javascript arrays properties duplicates


【解决方案1】:
myArray.map(elem => {
  if(typeof(elem.friends) == 'object') {
    elem.friends = [... new Set(elem.friends)]
    if(elem.friends.length == 1) {
      elem.friends = elem.friends[0]
    }
  }
});

【讨论】:

    【解决方案2】:

    我认为这个问题最简单的解决方案是始终用一个集合来表示friends 值。所以你的数组看起来像这样:

    const myArray = [
        {"id": 1, "value": 75, "friends": new Set([3])},
        {"id": 2, "value": 40, "friends": new Set([4])},
        {"id": 3, "value": 60, "friends": new Set([5])},
        {"id": 4, "value": 62, "friends": new Set([6, 1])},
        {"id": 5, "value": 55, "friends": new Set([1, 1])},
        {"id": 6, "value": 33, "friends": new Set([2, 1])}
    ];
    

    然后要添加朋友,您所要做的就是:

    myArray[i]["friends"].add(7);
    

    自动处理重复项。

    【讨论】:

      【解决方案3】:

      您可以编写一个函数来执行清理。

      const myArray = [
          {"id": 1, "value": 75, "friends": 3},
          {"id": 2, "value": 40, "friends": 4},
          {"id": 3, "value": 60, "friends": 5},
          {"id": 4, "value": 62, "friends": [6, 1]},
          {"id": 5, "value": 55, "friends": [1, 1]},
          {"id": 6, "value": 33, "friends": [2, 1]}
      ];
      
      const getDataWithoutDup = (data) => {
        // Loop through the array
        data.forEach(val => {
      
         // Check if friends property in the object is an array
         if(typeof(val.friends) == 'object') {
         let newVal = [...new Set(val.friends)];
      
         // Add the new value of friends
         val.friends = newVal.length > 1 ? newVal : newVal[0];
         }
        })
        console.log(data);
        return data;
      }
      
      getDataWithoutDup(myArray);

      【讨论】:

        【解决方案4】:

        试试这个。

        const myArray = [
            {"id": 1, "value": 75, "friends": 3},
            {"id": 2, "value": 40, "friends": 4},
            {"id": 3, "value": 60, "friends": 5},
            {"id": 4, "value": 62, "friends": [6, 1]},
            {"id": 5, "value": 55, "friends": [1, 1]},
            {"id": 6, "value": 33, "friends": [2, 1]}
        ];
        
        const friendList = myArray.map(getFriends);
        
        function getFriends(element) {
          if(element.friends.length > 0) {
              element.friends = [... new Set(element.friends)]
              if(element.friends.length === 1) {
                element.friends =  element.friends[0];
              }
           }
           return element;
        }
        
        
        console.log(friendList);

        【讨论】:

          猜你喜欢
          • 2019-12-12
          • 2021-06-18
          • 1970-01-01
          • 2016-11-08
          • 1970-01-01
          • 2020-06-29
          • 1970-01-01
          • 2015-09-24
          • 1970-01-01
          相关资源
          最近更新 更多