How to useLodash _.toArray() Method in Javascript

In this approach, we are using Lodash _.toArray() method that accept any value and convert it into an array.

Example: This example shows the implementation of the above-explained approach.

Javascript
// Requiring the lodash library 
const _ = require("lodash");

let myMap = new Map().set('GFG', 1).set('Geeks', 2);
let arr = _.toArray(myMap.keys());

// Use of _.toArray() method 
console.log(arr.length);

Output:

2

How to convert Map keys to an array in JavaScript ?

We are given a Map and the task is to get the keys of the map into an array using JavaScript. We can access the keys by using the Map keys() method.

We have a few methods to do this that are described below:

Table of Content

  • Approach 1: Using array.from() Method
  • Approach 2: Using for…of Loop Method
  • Approach 3: Using Map.forEach() Method
  • Approach 4: Using Lodash _.toArray() Method
  • Approach 5: Using map() Method

Similar Reads

Approach 1: Using array.from() Method

Declare a new map objectDisplay the map contentUse the keys() method on Map Object to get the keys of Map.Then use the array.from() method to convert a map object to an array....

Approach 2: Using for…of Loop Method

Create an empty arrayBy using the for…of Loop, we can iterate over the keysAt each iteration, we can push the key into an array....

Approach 3: Using Map.forEach() Method

Create an empty arrayUse Map.forEach() method for iterating over the loopthen push the keys into an array...

Approach 4: Using Lodash _.toArray() Method

In this approach, we are using Lodash _.toArray() method that accept any value and convert it into an array....

Approach 5: Using map() Method

Declare a new map object.Display the map content.Convert the map keys iterator into an array using the Array.from() method.Use the Array.prototype.map() method to transform the keys into an array....