Home > Software engineering >  Typescript Filter Array of Objects by Duplicate Properties
Typescript Filter Array of Objects by Duplicate Properties

Time:05-24

I have an array of objects I would like to filter to return objects with duplicate property values.

My Array of Objects:

values = [
  { id: 1, name: 'a' },
  { id: 2, name: 'b' },
  { id: 3, name: 'c' },
  { id: 4, name: 'a' }
];

The result I would like to have in a new array of objects:

duplicateValues = [
  { id: 1, name: 'a' },
  { id: 4, name: 'a' }
];

CodePudding user response:

const values = [{ id: 1, name: "a" }, { id: 2, name: "b" }, { id: 3, name: "c" }, { id: 4, name: "a" }];

const duplicates = Object.values(
  values.reduce((r, v) => ((r[v.name] ??= []).push(v), r), {})
)
  .filter((g) => g.length > 1)
  .flat();

console.log(duplicates);

TypeScript solution:

type Values = { id: number, name: string }[];

const values = [{ id: 1, name: "a" }, { id: 2, name: "b" }, { id: 3, name: "c" }, { id: 4, name: "a" }];

const duplicates = Object.values(
  values.reduce(
    (r: { [k: string]: Values }, v) => ((r[v.name] ??= []).push(v), r),
    {}
  )
)
  .filter((g) => g.length > 1)
  .flat();

console.log(duplicates);

CodePudding user response:

You can copy the array and filter them based on value. See the demo here: https://stackblitz.com/edit/typescript-xww2ai?file=index.ts

const values = [
  { id: 1, name: 'a' },
  { id: 2, name: 'b' },
  { id: 3, name: 'c' },
  { id: 4, name: 'a' },
];

const copyValues = [...values];

const duplicateObjects = values.filter(
  (v) => copyValues.filter((cp) => cp.name === v.name).length > 1
);
console.log(duplicateObjects);

CodePudding user response:

    function findDuplicates(data) {
      // map containing already added names.
      const existing = new Map();
      // set containing names, which were parsed in passed and added at future occurence of that name
      const consideredReadd = new Set();
      // final output array
      const output = [];
      data.forEach((item) => {
         if(!existing.has(item.name)) {
              // if not present in already parsed names, add in map
              existing.set(item.name, item);
         } else {
              if (!consideredReadd.has(item.name)) {
                  // if is already present and past record isn't added in output array, add that record in output array and set the key in data set.
                  consideredReadd.add(item.name);
                  output.push(existing.get(item.name));
              }
              // push the current item as it existed in past
              output.push(item);
         }
      });
      // return output array
      return output;
    }


    let values = [
      { id: 1, name: 'a' },
      { id: 2, name: 'b' },
      { id: 3, name: 'c' },
      { id: 4, name: 'a' }
    ];
    
    duplicateValues = findDuplicates(values);
    
    console.log(duplicateValues);

  • Related