Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.isObjectLike() method is used to find whether the given value is an object-like or not. It returns a True if the given value is object-like which means if it’s not null and has a type of the result of “object”. Otherwise, it returns false.
Syntax:
_.isObjectLike(value)
Parameters: This method accepts a single parameter as mentioned above and described below:
-
value: This parameter holds the value to check.
Return Value: This method returns true if the value is object-like, else false.
Note: Here, const _ = require(‘lodash’) is used to import the lodash library into the file.
Example 1:
// Requiring the lodash library const _ = require( "lodash" ); // Use of _.isObjectLike() method // When the value declare as an object console.log(_.isObjectLike({})); // When the value is null console.log(_.isObjectLike( null )); |
Output:
true false
Example 2:
// Requiring the lodash library const _ = require( "lodash" ); // Array object let arr = [1, 2, 3]; // Use of _.isObjectLike() method console.log(_.isObjectLike(arr)); |
Output:
true
Example 3:
// Requiring the lodash library const _ = require( "lodash" ); // The source object var info = { Name: "neveropen" , password: "gfg@1234" , username: "your_neveropen" } // Use of _.isObjectLike() method console.log(_.isObjectLike(info)); |
Output:
true
Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.
Reference: https://lodash.com/docs/4.17.15#isObjectLike