Related Articles

Related Articles

Lodash _.hasIn() Method
  • Last Updated : 09 Sep, 2020

The _.hasIn() method is used to check whether the path is a direct or inherited property of object or not. It returns true if path exists, else it returns false.

Syntax:

_.hasIn(object, path)

Parameters: This method accepts two parameters as mentioned above and described below:

  • object: This parameter holds the object to query.
  • path: This parameter holds the path to check. The path will be array or string.

Return Value: This method returns true if path exists, else false

Example 1:



Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the lodash library  
const _ = require("lodash");  
  
// Given object
var object =  _.create({ 'a': _.create({ 'b': 2 }) });
  
// Use of _.hasIn method 
console.log(_.hasIn(object, 'a')); 
console.log(_.hasIn(object, ['a'])); 
console.log(_.hasIn(object, ['b'])); 

chevron_right


Output:

true
true
false

Example 2:  

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the lodash library  
const _ = require("lodash");  
  
// Given object
var object =  _.create({ 'a': _.create({ 'b': 2 }) });
  
// Use of _.hasIn method 
console.log(_.hasIn(object, 'a.b')); 
console.log(_.hasIn(object, ['a','b'])); 
console.log(_.hasIn(object, ['a','b','c'])); 

chevron_right


Output:

true
true
false

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :