简体   繁体   中英

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

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.

Below is the logic i am trying to use but it doesn't work if ids are null

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.

Since array is an array, do a reduce to iterate over the array. If the id is nuil or not found in filterKeys , push the item to the accumulator and return it for the next iteration. Since we don't care about ids that are null, they don't have an effect and are thus not filtered out.

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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