This API looks very sexy, don't get me wrong, but it has some flaws:
You would have to know 100% of all the properties of the object to search the properties from, this is not always possible, especially from large code-bases.
If you use an object from an untrusted source or an API, nothing guarantees that you won't get something different tomorrow, and this could crash your application if you are using the same end as one of the property of this API.
Even if you handle the JavaScript exception thrown from accessing a bad index, this totally blocks the user from getting the expected behavior, while the solution provided by the OP (and some other libraries like Lodash) would work as expected.
But if you are using an object from a trusted source, then your solution is very elegant.
You would have to know 100% of all the properties of the object to search the properties from, this is not always possible, especially from large code-bases.
This claim is false, no matter the size of code-bases, you always have to know where to access the right field in that JSON. Knowing the JSON field location already avoided a bad ending.
If you use an object from an untrusted source or an API, nothing guarantees that you won't get something different tomorrow, and this could crash your application if you are using the same end as one of the properties(your typo) of this API.
The origin of the problem above comes from the untrusted source, not my provided solution. Optional chaining can't solve it either.
Reminder: All of my solutions do avoid crashing from accessing bad indices.
Even if you handle the JavaScript exception thrown from accessing a bad index, this totally blocks the user from getting the expected behavior, while the solution provided by the OP (and some other libraries like Lodash) would work as expected.
Optional chaining is the topic of this article, which means no exception is thrown from accessing a bad index. Have you tried to access a bad index from my solutions yet? It behaves as expected.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This is far better than the one provided by OP . Thanks
Until you have a property named
_
that you can't access anymore.Thanks for pointing out, feel free to try this.
This API looks very sexy, don't get me wrong, but it has some flaws:
But if you are using an object from a trusted source, then your solution is very elegant.
This claim is false, no matter the size of code-bases, you always have to know where to access the right field in that JSON. Knowing the JSON field location already avoided a bad ending.
The origin of the problem above comes from the untrusted source, not my provided solution. Optional chaining can't solve it either.
Reminder: All of my solutions do avoid crashing from accessing bad indices.
Optional chaining is the topic of this article, which means no exception is thrown from accessing a bad index. Have you tried to access a bad index from my solutions yet? It behaves as expected.