lodash has method
This is one of many methods in lodash that accept a string form of a object path to a value. Another such method of note in lodash would be the _.get, and _.set methods. This is not one of the most compelling methods in lodash, in fact when it comes down to it there are only really a handful that I find myself still using in projects. Still when it comes to gaining a comprehensive understanding of everything available within lodash it makes sense to not just write about the popular, or often used methods. Also maybe there are some situations in which it is nice to test for a object property by way of a string.
1 - lodash has in a nut shell
So the lodash has method can be used by just passing an object followed by a string that represents the path of a property of an object. In the event that the property is there it will return true, else it will not.
So this object method can be used as a way to feature test if a path exist in an object and if not can be used with another method like the lodash set method to set the path. If you are more interested in what lodash has to offer in general you might want to check out my main post on lodash.
2 - AutoHeal example of lodash has
Now that I have covered the basics of what the lodash has method does maybe it is called for for at least one example of why the lodash has method, or something like it, might come in handy. Say I have this project where I am dealing with a type of object that may, or may not have a bunch of nested objects as one of its properties. I can not just assume that the property will always be there and just get the value, that would result in an error. So I must test for the property first before attempting to get any value that might be there.
Although this does work okay, it does strike me as being a bit messy. I also would have to do this in every part of the body of code of the project where doing this kind of check is required. As such it would be nice to have a way to make this kind of thing a little bit more clean and readable.
2.2 - Using lodash has in the apply auto heal method
By making use of the lodash has method this makes the code of my apply auto heal method for more concise.
There is the question of performance would take a hit or not, maybe it would, maybe not, it is true that more concise solutions to a problem are not always the best course of action. However for me the first and foremost thing is to get something up and running first, and worry about those details later. I also generally think that readability should take priority over performance at least when it comes to the alpha state of a project anyway. With that said I do like this solution better, it is a lot cleaner, and it is just nice to be able to make these kinds of checks like this.
3 - Conclusion