lodash has method

This will be a quick post on the lodash has method, a simple object method that can be used to check if an object has a certain path to it or not. 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.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
let obj = {
foo: {
bar: {
x: 40,
y: 2
}
},
dust: {
in: {
the: {
wind: 'that is all we are'
}
}
}
};
console.log( _.has(obj, 'dust.in.the.wind') ); // true
console.log( _.has(obj, 'dust.in.x') ); // false
console.log( _.has(obj, 'foo.bar.x') ); // true

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.