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

216
Views
How to parse duplicate data in a list of objects based on multiple keys having the same value, where one key has a value of an array of objects

As the title states, I am trying to store a list of duplicate objects based on specific key:value pairs being the same.

The goal is to parse the information and return a list of duplicate items. A duplicate item is when the ID , title, items are a match. The main problem I'm having with this is that inside each object, one of the keys to compare is an array holding a list of objects as values.

How would you go about this problem, looping through an array of objects to determine if the items array are duplicate?

Below is just a small example of the JSON data.

Should return : whole object of pk:"0002" and pk"0003" since they are duplicate. Should not return Object pk:"0001" and pk "0004" since their array of items:[{}] are not equal.

My attempt can determine if the ID and titles are a match but I can't get the items to match:

var data = [
{ pk: "0001", ID: "1A", title: "Material 15", items: [{ title: "Info", value: "" }, { title: "Type", value: "" }, { title: "EXTRA", value: "55" }, ], }, 
{ pk: "0002", ID: "1A", title: "Material 1", items: [{ title: "Info", value: "" }, { title: "Type", value: "" }, ], }, 
{ pk: "0003", ID: "1A", title: "Material 1", items: [{ title: "Info", value: "" }, { title: "Type", value: "" }, ], }, 
{ pk: "0004", ID: "1A", title: "Material 15", items: [{ title: "Info", value: "" }, { title: "Type", value: "" }, ], }, 
];


let duplicates = [];
data.forEach((el, i) => {
  data.forEach((element, index) => {
    if (i === index) return null;
    if (element.ID === el.ID && element.title === el.title) {
      if (!duplicates.includes(el)) {
        //Issue here -> I want to compare the array of items but im not sure how to ?
        //Attempt:
        // I think I understand that this is wrong due to the indexes not sure how you would find this ?

        // if (element.items[i] === el.items[i]) {
        //   duplicates.push(el);
        //   console.log("Test");
        // }

        duplicates.push(el);
      }
    }
  });
});
console.log("duplicates", duplicates);

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could count the items with same stringified values and gwt their objects as result.

const
    data = [{ pk: "0001", ID: "1A", title: "Material 15", items: [{ title: "Info", value: "" }, { title: "Type", value: "" }, { title: "EXTRA", value: "55" }] }, { pk: "0002", ID: "1A", title: "Material 1", items: [{ title: "Info", value: "" }, { title: "Type", value: "" }] }, { pk: "0003", ID: "1A", title: "Material 1", items: [{ title: "Info", value: "" }, { title: "Type", value: "" }] }, { pk: "0004", ID: "1A", title: "Material 15", items: [{ title: "Info", value: "" }, { title: "Type", value: "" }] }],
    keys = ['ID', 'title', 'items'],
    map = data.reduce((m, o) => {
        const
            key = keys.map(k => JSON.stringify(o[k])).join('|'),
            [count = 0, array = []] = m.get(key) || [];
        return m.set(key, [count + 1, [...array, o]]);
    }, new Map),
    duplicates = Array
        .from(map.values(), ([count, array]) => count === 1 ? []: array)
        .flat();

console.log(duplicates);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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!