let input = [
{
id: 1,
rollNo: 5,
Name: "A",
stDetails: {
city: "city1",
pin: 1234,
},
},
{
id: 2,
rollNo: 5,
Name: "B",
stDetails: {
city: "city1",
pin: 1234,
},
},
{
id: 3,
rollNo: 5,
Name: "C",
stDetails: {
city: "city1",
pin: 1234,
},
},
{
id: 4,
rollNo: 5,
Name: "D",
stDetails: {
city: "city1",
pin: 1234,
},
},
{
id: 5,
rollNo: 10,
Name: "E",
stDetails: {
city: "city2",
pin: 1234,
},
},
];
let uniqueKey = ["rollNo", "stDetails.city"];
I want to get the id
of all non-unique values in more than one occurrence and to check record is unique we need to check by rollNo
and stDetails.city
So the expected output is:
[{id:2},{id:3},{id:4}];
Here you can see id
2,3 and 4 have the same details as id
1 so that’s why it is expected output
2
Answers
You can do it with this way.
Pure JS Solution
Of course you can use libraries but pure ones are always the best!
You should loop through the
input
then save unique ones and compare each one touniques
array!If you have trouble with comparison of objects, simply convert it to
JSON
!Comment if you have questions…
Notice: If you needed the unique ones, print the
uniques
array instead ofnon_uniques
array.