I have a json object:
var object1 = [{
"value1": "1",
"value2": "2",
"value3": "3",
},
{
"value1": "1",
"value2": "5",
"value3": "7",
},
{
"value1": "6",
"value2": "9",
"value3": "5",
},
{
"value1": "6",
"value2": "9",
"value3": "5",
}]
Now I want to take each record out of that object and check how many times exact copy of that record is appearing in that object. If it is only 1 copy do something and if it is more than 2 do something else.
There are few answers on JSON duplicates but they target specific value not full record.
So I will take the record:
{
"value1": "1",
"value2": "2",
"value3": "3",
}
and compare it against the object. The above record will return 1 as there is only 1 copy inside the object
For Future use. The full answer would be:
var asset = [
{ value1: "1", value2: "2", value3: "3" },
{ value1: "1", value2: "5", value3: "7" },
{ value1: "6", value2: "9", value3: "5" },
{ value1: "6", value2: "9", value3: "5" }
];
function countEqual(oo, pp) {
var count = 0;
oo.forEach(function (el) {
var i, equal = true;
for (i in el) {
equal = equal && el[i] === pp[i];
}
equal && count++;
});
return count;
}
var cleaned = [];
asset.forEach(function (itm) {
var unique = true;
cleaned.forEach(function (itm2) {
if (_.isEqual(itm, itm2)) unique = false;
});
if (unique) cleaned.push(itm);
});
for (var i = 0; i < cleaned.length; i++)
{
if(countEqual(asset, cleaned[i]) === 1)
{
// DO SOMETHING
}
else
{
// DO SOMETHING ELSE
}
}