Saturday, October 18, 2025
HomeLanguagesJavascriptLodash _.flatMapDepth() Method

Lodash _.flatMapDepth() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, objects, numbers etc.

The _.flatMapDepth() method creates a flattened array of values by running each element in the given collection through the iteratee function. It recursively flattens the mapped results up to the given depth value. It is similar to the _.flatMap() method.

Syntax:

_.flatMapDepth( collection, iteratee, depth )

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

  • collection: It is the collection to iterate over.
  • iteratee: It is the function that is invoked per iteration.
  • depth: It is a number that specifies the maximum recursion depth. It is an optional parameter. The default value is 1.

Return Value: This method returns the new flattened array.

Example 1:




// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
var users = ([3, 4]);
  
// Using the _.flatMapDepth() method
let flat_map = _.flatMapDepth(users, duplicate, 2 )
function duplicate(n) {
  return [[[n, n]]];
}
  
// Printing the output 
console.log(flat_map);


Output:

[ [ 3, 3 ], [ 4, 4 ] ]

Example 2:




// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
var users = (['q', 'r', 't', 'u']);
  
// Using the _.flatMapDepth() method
let flat_map = _.flatMapDepth(users, duplicate, 2 )
function duplicate(n) {
  return [[[n, n]]];
}
  
// Printing the output 
console.log(flat_map);


Output:

[ [ 'q', 'q' ], [ 'r', 'r' ], [ 't', 't' ], ['u', 'u' ] ]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS