【问题标题】:Filter an array with objects过滤包含对象的数组
【发布时间】:2021-08-07 03:21:47
【问题描述】:

我有以下数组:


arr = [
    [
        {
            id: 1,
            name: 'name 1',
            pushname: 'name 1'
        }
    ],
    [
        {
            id: 2,
            name: 'name 2',
            pushname: 'name 2'
        }
    ],
    [
        {
            id: 1,
            name: 'name 1',
            pushname: 'name 1'
        }
    ]
]

/*return:
 [
  [ { id: 1, name: 'name 1', pushname: 'name 1' } ],
  [ { id: 2, name: 'name 2', pushname: 'name 2' } ],
  [ { id: 1, name: 'name 1', pushname: 'name 1' } ]
]

*/

如何删除重复的信息?

我想退货:

/*
 [
  [ { id: 1, name: 'name 1', pushname: 'name 1' } ],
  [ { id: 2, name: 'name 2', pushname: 'name 2' } ]
]
*/

我相信我会使用过滤器,但我尝试了多种方式,但没有找到解决方案。有人知道如何帮助我吗?

注意:我是初学者!对不起,如果问题重复,我会排除它!

【问题讨论】:

  • “我尝试了多种方式,但没有找到解决方案”,请将这些尝试添加到您的问题中,以便我们可以解决。
  • 这应该会有所帮助:stackoverflow.com/questions/45439961/…
  • @Ajay 我尝试使用发送给我的链接,但没有成功。返回给我的金额只是[ [ { id: 1, name: 'name 1', pushname: 'name 1' } ] ]

标签: javascript


【解决方案1】:

所以根据你的情况,这会有所帮助

arr = [
  [
    {
      id: 1,
      name: "name 1",
      pushname: "name 1",
    },
  ],
  [
    {
      id: 2,
      name: "name 2",
      pushname: "name 2",
    },
  ],
  [
    {
      id: 1,
      name: "name 1",
      pushname: "name 1",
    },
  ],
];

// Create a reference Array
let visitedID = [];

// Perform filter operation
arr = arr.filter((element) => {
//   check if the value is present (repetetion)
  if (visitedID.includes(element[0].id)) {
    return false;
  } else {
    // Push the data to the array and return
    visitedID.push(element[0].id);
    return true;
  }
});

console.log(arr);

解释: 所以要让这个条件起作用,你必须确保元素的 ID 是唯一的,我们基本上创建一个 ID 数组并根据 ID 以及它们是否在visitedID 数组中可用来过滤它们。

我也很困惑,为什么你要创建一个 Array[Array[Object]] 而不是,我们可以使用 Array[Objects] 并且过滤器部分也很容易。如果您想更改表格,可以使用 cmets 中提到的答案。

编辑: 您可以从这个问题中遵循这种速记方法。这应该有帮助 Remove duplicate values from an array of objects in javascript

【讨论】:

    【解决方案2】:

    所以.. 首先要做的事情。

    在 JS 中比较数组有点棘手,如果您想了解更多信息,我会向您推荐这个问题:How to compare arrays in JavaScript?

    我对这个问题的解决方案如下:

    function removeDuplicates(source) {
     const newArray = [];   //Create a new array where the results are stored as String
     for(let element of source){ //Iterate through the provided array (source)
        const stringified = JSON.stringify(element) //Convert the current element into a JSON String
        if(!newArray.includes(stringified)) //Check if the Element is alread added into the new List, if not add it
            newArray.push(stringified) //Add it here
     }
    
    return newArray.map(JSON.parse); //Convert the String-Array into an Object-Array and return
    }
    

    这可能不是最优雅的解决方案,但你得到了一个特殊情况,即你有一个数组,其中包含使事情复杂化的数组。

    希望我能提供帮助 :) 如果您有后续问题,请随时提问!

    【讨论】:

      猜你喜欢
      • 2019-07-30
      • 2021-08-28
      • 2017-01-28
      • 2022-08-15
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多