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. If you are more interested in what lodash has to offer in general you might want to check out my mian post on lodash in general.

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.