I am having array of objects in a following form,
let test = [
{
RowCreationMethod: "excel",
deviceID: "123ECd9",
deviceName: "ZENW42NLPC0378",
devicePrimary: "Yes",
deviceType: "Laptop",
employeeID: 101,
isExists: "No",
isRescheduled: "No",
vipUser: "Yes"
},{
RowCreationMethod: "excel",
deviceName: "ZENW42NLPC0378",
devicePrimary: "Yes",
deviceType: "Laptop",
deviceID: "123ECd9",
employeeID: 101,
isExists: "No",
isRescheduled: "No",
vipUser: "Yes"
}];
I have written following code to check if their exits any duplicate object in my array its working but if I changes the sequence of any object properties then its not working, because in my code I am converting objects to string and verifying it.
Here is my code
let final = []
test.forEach(x => {
if(!final.some(y => JSON.stringify(y) === JSON.stringify(x))){
final.push(x)
}
})
JSON.stringify(Object.values(y).sort()). Worst-case: loop though the object properties and compare each to each. If you do that, run your existing JSON compare and sub-iterate only if false to keep it fast when possible. You can also use [].filter instead of forEach to collect the uniques w/o [].push().Object.keys()to get an array of the keys in the objects. You can check if the array lengths are equal, and then check if their values in both objects match.