I have two objects with multiple properties and with multiple layers. At different layers, I might have some arrays. I want to be able to compare the two objects, disregarding the ordering of elements in each array.
Example 1: object1 and object2 should be "equal"
object1 = {
person: "John",
children: [
{
person: "Anne"
},
{
person: "Mark"
}
]
};
object2 = {
person: "John",
children: [
{
person: "Mark"
},
{
person: "Anne"
}
]
};
// This would have to return true:
expect(object1).to."fundamentally".equal(object2);
Example2: a bit more complicated, object1 and object2 should still be the same
object1 = {
person: "John",
children: [
{
person: "Anne"
},
{
person: "Mark",
pets: [
{
species: "cat",
name: "Purr"
},
{
type: "labrador",
name: "Woofy",
age: 3
}
]
}
],
cars: [
{
brand: "BMW",
year: 2017
},
{
brand: "Mercedes-Benz",
year: 2020
}
]
};
object2 = {
person: "John",
children: [
{
person: "Mark",
pets: [
{
type: "labrador",
name: "Woofy",
age: 3
},
{
species: "cat",
name: "Purr"
}
]
},
{
person: "Anne"
}
],
cars: [
{
brand: "Mercedes-Benz",
year: 2020
},
{
brand: "BMW",
year: 2017
}
]
};
// This would have to return true:
expect(object1).to."fundamentally".equal(object2);
Even though the children, cars and pets are in different order, I still want to recognize that both objects have the same contents.
Is there a function that would do this?