Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
Typescript Filter Array of Objects by Duplicate Properties

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' }
];
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

  • Group the values by their name using Array.prototype.reduce
  • Filter the groups where the length is greater than 1 using Array.prototype.filter.
  • And finally flatten the resultant array using Array.prototype.flat.

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);
about 4 years ago · Juan Pablo Isaza Report

0

    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);

about 4 years ago · Juan Pablo Isaza Report

0

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);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!