Comparing objects in JavaScript can be quite challenging as there is no straightforward built-in function for it. It's important to first define what "equals" or "exists" means in the context of your specific situation.
For instance, if you know that the objects you want to compare always have identical keys, you could consider them equal if their values match. Here's an example:
function checkEquality(obj1, obj2) {
return (
obj1.code === obj2.code && obj1.value === obj2.value
)
}
You can then create a function to check this equality for each element in a list:
function listEquality(list1, list2) {
return list1.every(item1 =>
list2.reduce((acc, item2) =>
acc || checkEquality(item1, item2)
, false)
);
}
By comparing the `event` object with `event1`, the result would be `true`:
listEquality(event, event1)
> true
You can also use this function to determine if an `event` exists in a list of `events`:
function isInList(targetObject, objectList) {
return objectList.some(object => listEquality(targetObject, object));
}
isInList(event, combination)
> false
isInList(event1, combination)
> false
isInList(combination[0], combination)
> true
If your comparison requirements are more complex, utilizing established solutions from libraries like lodash might be more beneficial. Lodash offers functions such as isEqual
and isEqualDeep
for checking object equality. You can find more information in the documentation.
I hope this explanation proves helpful.