简体   繁体   English

从对象数组中删除重复项

[英]Removing Duplicates from Array of Objects

Hi I m trying to remove duplicates from array of object using id, but id's are null then the object should contain those null id's and remove others which are duplicate嗨,我正在尝试使用 id 从对象数组中删除重复项,但 id 为空,则该对象应包含那些空 id 并删除其他重复项

Here is the array of objects example:这是对象数组示例:

const arr = [
  {
    id: 6652,
    value: "erger"
  },
  {

    id: 6652,
    value: "sdfs"
  },
  {

    id: 6653,
    value: "sdgdfg"
  },
  {

    id: 6000,
    value: "trgd"
  },
  {

    id: 6667,
    value: "asdf"
  },
  {

    id: 6667,
    value: "fdg"
  },
  {

    id: 6668,
    value: "dfgr"
  },
  {

    id: null,
    value: "fg"
  },
  {

    id: null,
    value: "dfgdf"
  },

  {

    id: null,
    value: "fg"
  },
  {

    id: null,
    value: "dfgdf"
  }
];

Below is the finalResult下面是最终结果

 array = [{ id: 6652 value: "sdfs" }, { id: 6653 value: "sdgdfg" }, { id: 6000 value: "trgd" }, { id: 6667 value: "fdg" }, { id: 6668 value: "dfgr" }, { id: null value: "fg" }, { id: null value: "dfgdf" }, { id: null value: "fg" }, { id: null value: "dfgdf" } ]

In the above result the 6652 and 6667 is removed as they were duplicates and but null id are kept as i don't want to remove the null id and remove other repeated values.在上面的结果中,6652 和 6667 被删除,因为它们是重复的,但空 id 被保留,因为我不想删除空 id 并删除其他重复值。

Below is the logic i am trying to use but it doesn't work if ids are null下面是我尝试使用的逻辑,但如果 ids 为空则它不起作用

array= array.filter((v,i,a)=>a.findIndex(t=>( v.id !== null && t.id === v.id ))===i) array= array.filter((v,i,a)=>a.findIndex(t=>( v.id !== null && t.id === v.id ))===i)

const filterKeys = {};
const filtered = arr.reduce((acc, item) => {
  if (item.id === null || !(item.id in filterKeys)) {
    filterKeys[item.id] = true;
    acc.push(item);
  }

  return acc;
}, []);

Create a separate object to keep track of object ids that have been encountered, filterKeys in this case.创建一个单独的对象来跟踪遇到的对象 ID,在本例中为filterKeys

Since array is an array, do a reduce to iterate over the array.由于array是一个数组,所以执行一个 reduce 来迭代该数组。 If the id is nuil or not found in filterKeys , push the item to the accumulator and return it for the next iteration.如果id为 null 或在filterKeys未找到, filterKeys项目推送到累加器并将其返回以用于下一次迭代。 Since we don't care about ids that are null, they don't have an effect and are thus not filtered out.由于我们不关心为空的 id,它们没有效果,因此不会被过滤掉。

Use this below code I tested(working) with your array of objects :使用下面我测试(工作)的代码与您的对象数组:

    arr.forEach(function (item) {

        if (item.id == null) {
            result.push(item);
        }
        else {
            var index = result.findIndex(x => x.id == item.id);
            if (index == -1)
            {
                result.push(item);
            }
           
        }
    });
    console.log(result)

You can try this out-你可以试试这个——

 const array = [{ id: 6652, value: 'erger' },{ id: 6652, value: 'sdfs' },{ id: 6653, value: 'sdgdfg' },{ id: 6000, value: 'trgd' },{ id: 6667, value: 'asdf' },{ id: 6667, value: 'fdg' },{ id: 6668, value: 'dfgr' },{ id: null, value: 'fg' },{ id: null, value: 'dfgdf' },{ id: null, value: 'fg' },{ id: null, value: 'dfgdf' },]; let index = 0; const result = Object.values( array.reduce( (acc, curr) => curr.id === null ? { ...acc, [index++]: curr } : { ...acc, [curr.id]: curr }, {} ) ); console.log(result);
 .as-console-wrapper {min-height: 100%; top: 0}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM