how to use the ismap function from lodash in javascript

The _.ismap function in Lodash checks if a given value is a Map object.

To use the _.ismap function, first, we need to install Lodash in our project using npm or yarn.

npm install lodash

# or 

yarn add lodash
43 chars
6 lines

Then we can import the _.ismap function where we want to use it.

index.tsx
// Importing the ismap function from lodash
const isMap = require('lodash/ismap');

// Creating a Map
const myMap = new Map([[1, 'one'], [2, 'two'], [3, 'three']]);

// Checking if the given object is Map using isMap function
console.log(isMap(myMap)); // true

// Providing other type of object to isMap function
console.log(isMap({'a': 1, 'b': 2})); // false
361 chars
12 lines

In the above code sample, we first import the _.ismap function using require.

Then, we create a Map object myMap.

Finally, we call the isMap function with myMap as an argument and it returns true since myMap is an instance of Map. We also call isMap function with a non-Map object to check if it returns false.

gistlibby LogSnag