I would like to find duplicate data sets from below payload using combination of 'NAME', 'ID'. If a set exist more than 3 times, I need to return NAME, ID of duplicated data set.
{
"Test": "1",
"value": [
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "EFGH",
"ID": "5678",
"ACTIVE": "true"
},
{
"NAME": "EFGH",
"ID": "5678",
"ACTIVE": "true"
},
{
"NAME": "EFGH",
"ID": "5678",
"ACTIVE": "true"
},
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "IJKL",
"ID": "91011",
"ACTIVE": "true"
}
]
}
Expected output:
["ABCD:1234", "EFGH:5678"]
Try this. You can improve this further by performance wise, if you work around a bit.
var data = {
"Test": "1",
"value": [
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "EFGH",
"ID": "5678",
"ACTIVE": "true"
},
{
"NAME": "IJKL",
"ID": "91011",
"ACTIVE": "true"
}
]
};
var objArray = data.value;
var duplicates = []; // duplicates will be stored here.
for(var i=0, iLen = objArray.length; i<iLen;i++){
var obj = objArray[i];
var filtered = objArray.filter(function(arrVal) {
return arrVal.NAME === obj.NAME && arrVal.ID === obj.ID ;
});
var dup = obj.NAME + ":" + obj.ID;
if(filtered.length>=3 && duplicates.indexOf(dup) < 0) {
duplicates.push(dup);
}
}
this questuon was answered multiple times on SO. You create array from the json and compare the elements of the array.
According to your sample output I believe it's "at least 3 times" rather than "more than 3 times".
Below snippet can product the expected output with the sample data.
const data = {
"Test": "1",
"value": [
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "EFGH",
"ID": "5678",
"ACTIVE": "true"
},
{
"NAME": "EFGH",
"ID": "5678",
"ACTIVE": "true"
},
{
"NAME": "EFGH",
"ID": "5678",
"ACTIVE": "true"
},
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "IJKL",
"ID": "91011",
"ACTIVE": "true"
}
]
};
const occurrence = {}; // key: count
const result = [];
for (const item of data.value) {
const key = `${item.NAME}:${item.ID}`;
occurrence[key] = (occurrence[key] || 0) + 1;
if (occurrence[key] >= 3) {
result.push(key);
}
}
console.log(result);