A cleaner way to check the values ​​of objects is layered

I thought if there is a way to avoid

if (this.a && this.a.hasOwnProperty('b') && this.a.b.hasOwnProperty('c')) {
    // this.a.b.c exists
}

and make it much easier to encode and read, for example

if ('b.c' in this.a) {
    // same, this.a.b.c exists
}

or

if ('a.b.c' in this) ...

It’s good that it’s not so painful to write a few more characters, but there may be more complex objects that need to be checked at each level to make sure they are installed. A few years ago I didn’t even think that such a method is possible, but recently, when ES6 is here and everything becomes easier, I hope for a good solution.

EDIT:

Thank you for linking a message that is close to what I asked, did not see it. However, I saw some others similar to him. As her 7 years old, I am not sure of her recent condition and still want to ask for new solutions, if any.

+4

Source: https://habr.com/ru/post/1691316/


All Articles