I am using the below code to get the count of duplicate objects that are in an array.
I am querying through a table which has groups and it's complexity. I am passing the groups and its complexities to an object. I have duplicate entries like
[
{group: A, complexity: Simple},
{group: A, complexity: Complex},
{group: A, complexity: Simple},
{group: B, complexity: Simple},
{group: A, complexity: Medium}
]
I need the count of duplicates like below
[
{group: A, complexity: Simple}: 2,
{group: A, complexity: Complex}: 1,
{group: B, complexity: Simple}: 1,
{group: C, complexity: Medium}: 1
]
var data = {};
var app = [];
for(let i=0; i<=2; i++){
var fName = "Ram";
var lName = "Krishna";
data = { "ID": fName, "Status": lName };
app.push(data);
}
var call = JSON.stringify(duplicate(app));
console.log("call: " + call);
function duplicate(app)
{
var counts = {};
for(var i =0; i < app.length; i++)
{
var ab = JSON.stringify(app[i]);
if (counts[ab])
{
counts[ab] += 1;
}else{
counts[ab] = 1;
}
}
return counts;
}
I am getting the below output
call: {"{\"ID\":\"Ram\",\"Status\":\"Krishna\"}":3}
What I am expecting
call: {"{"ID":"Ram","Status":"Krishna"}":3}
Can anyone please help me with this?