Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

226
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda