I have an issue which I dont’t really know how to tackle in a good way
I have an array of objects which looks roughly like this:
[
{ name: "horse", newName: "owl" }
{ name: "owl", newName: "horse" }
{ name: "frog", newName: "dog" }
]
I want to remove “mirrored” objects from this array, in result having an array like this:
[
{ name: "frog", newName: "dog" }
]
Basically I need to find objects with opposite keys and values
More complex scenarios:
[
{ name: "horse", newName: "frog" }
{ name: "owl", newName: "horse" }
{ name: "frog", newName: "owl" }
]
// Result: []
[
{ name: "horse", newName: "frog" }
{ name: "dog", newName: "cat" }
{ name: "owl", newName: "horse" }
{ name: "frog", newName: "owl" }
{ name: "monkey", newName: "worm" }
{ name: "cat", newName: "dog" }
]
// Result: [{ name: "monkey", newName: "worm" }]
In the first case I would simply loop through the array and if an object like this is found:
{key: obj1.value, value: obj1.key}
I would splice them both
But I have no idea how to approach the more complex situation when 3 or more objects would have to be removed. Any hints?
In advance thanks for your time