Within my data, I have 6 different types of objects (some with double nested arrays), with varying numbers of entries, as long as each entry is unique.
These objects do not possess a consistent unique identifier (one is applied on submission in the backend).
Here is an example array with 2 object types:
arr = [
{name:"aaa",time:15},
{name:"aaa",time:22},
{timeline: "250", chars[{a},{b},{c}]},
{timeline: "220", chars[{d},{e},{f}]},
]
obj = {name:"aaa",time:22}
My goal is to determine if obj
exists in arr
, resulting in a true
or false
output.
I have attempted the following methods:
- I received a recommended method that resulted in an error:
#<Object> is not a function
console.log(arr.find(obj))
- Another suggestion I found always returns false, even when the element is present
console.log(arr.includes(object))
- My personal attempt at a method always fails.
console.log(arr.filter((element, index) => element === obj)
Regarding attempt 4
, comparing only the name
field is inadequate as it overlooks unique times essential for valid entries.
Providing every field for comparison would not work either, as each object may or may not have certain fields, leading to errors.
Manually pre-filtering into distinct categories is impractical, as each new type added would require manual inclusion in the filter.
If you are aware of a library that can handle this, please share, as it would be ideal. Alternatively, any other suggestions (excluding separating arrays) are welcome.