Assuming you want to remove duplicate objects from an array that has nested objects, you can use the following process in ES5.6:
Here's an example code snippet:
var data = [
{ name: "John", age: 25, address: { city: "New York", country: "USA" } },
{ name: "Jane", age: 30, address: { city: "London", country: "UK" } },
{ name: "John", age: 25, address: { city: "New York", country: "USA" } }
];
var stringifiedData = JSON.stringify(data);
var uniqueString = [...new Set(stringifiedData)];
var uniqueData = JSON.parse(uniqueString);
console.log(uniqueData); //[{name: "John", age: 25, address: { city: "New York", country: "USA" }}, {name: "Jane", age: 30, address: {city: "London", country: "UK"}}]
Note that this approach relies on the string representation of the objects within the array. As a result, it may not work effectively with complex objects containing functions, Date objects or circular references.
Asked: 2022-04-02 11:00:00 +0000
Seen: 8 times
Last updated: Mar 17 '23