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