Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Mapping with Controlled Flattening:
4.2.
JavaScript
4.3.
Transforming Nested Arrays with Specific Depth:
4.4.
JavaScript
4.5.
Handling Complex Nested Structures:
4.6.
JavaScript
4.7.
Flattening to Maximum Depth:
4.8.
JavaScript
5.
Frequently Asked Questions 
5.1.
How does _.flatMapDepth() differ from _.flatMapDeep()?
5.2.
Can _.flatMapDepth() be used for non-numeric depths?
5.3.
Is _.flatMapDepth() efficient for handling large or complex arrays?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.flatMapDepth() Method

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The versatility of array manipulation in JavaScript is further enhanced by Lodash's _.flatMapDepth() method. This function extends the capabilities of _.flatMap() by allowing control over the depth of flattening after mapping. 

Lodash _.flatMapDepth() Method

This article delves into the _.flatMapDepth() method, exploring its syntax, applications, and benefits through illustrative examples and FAQs.

Why This Function is Used

The _.flatMapDepth() function is used when there is a need to map each element in a collection and then flatten the result up to a specified depth. It is particularly useful in scenarios involving nested arrays where both element transformation and controlled flattening are required. This method provides flexibility in handling complex data structures by allowing precise control over the level of flattening after mapping.

Syntax, Parameter and Return Value

Syntax: 

_.flatMapDepth(collection, [iteratee=_.identity], [depth=1])

Parameters:

  • collection (Array|Object): The collection to iterate over.
     
  • [iteratee=_.identity] (Function): The function invoked per iteration.
     
  • [depth=1] (number): The maximum recursion depth.

Return Value: 

(Array) - Returns the new flattened array.

Examples 

Mapping with Controlled Flattening:

  • JavaScript

JavaScript

var _ = require('lodash');

var numbers = [1, [2, [3, [4]]]];

var flatMapTwoLevels = _.flatMapDepth(numbers, n => [n, n * 2], 2);

console.log(flatMapTwoLevels);
You can also try this code with Online Javascript Compiler
Run Code

Output:

 [1, 2, [2, 4, [3, 6, [4, 8]]]]


Demonstrates mapping numbers and flattening two levels deep.

Transforming Nested Arrays with Specific Depth:

  • JavaScript

JavaScript

var nestedArray = [[1, 2], [[3, 4], [5, 6]]];

var flatMappedTwoLevels = _.flatMapDepth(nestedArray, x => x.map(n => n + 1), 2);

console.log(flatMappedTwoLevels);
You can also try this code with Online Javascript Compiler
Run Code

 Output:

 [2, 3, [4, 5, 6, 7]]


Shows mapping and flattening a nested array to a specified depth.

Handling Complex Nested Structures:

  • JavaScript

JavaScript

var complexStructure = [1, [2, [3, [4, [5]]]]];

var transformedTwoLevels = _.flatMapDepth(complexStructure, n => [n, n * n], 2);

console.log(transformedTwoLevels);
You can also try this code with Online Javascript Compiler
Run Code

 Output: 

[1, 1, [2, 4, [3, 9, [4, [5]]]]]


Illustrates complex mapping and controlled flattening of a nested structure.

Flattening to Maximum Depth:

  • JavaScript

JavaScript

var multiLevelArray = [1, [2, [3, [4]]]];

var flatMapMaxDepth = _.flatMapDepth(multiLevelArray, n => [n, n * 3], Infinity);

console.log(flatMapMaxDepth);
You can also try this code with Online Javascript Compiler
Run Code

Output:

 [1, 3, 2, 6, 3, 9, 4, 12]


Demonstrates using Infinity for the deepest possible flattening after mapping.

Frequently Asked Questions 

How does _.flatMapDepth() differ from _.flatMapDeep()?

While _.flatMapDeep() flattens to the deepest level, _.flatMapDepth() allows specifying the depth of flattening, giving more control over the resulting array structure.

Can _.flatMapDepth() be used for non-numeric depths?

The depth parameter should be a non-negative integer; non-numeric values may lead to unexpected results or no flattening.

Is _.flatMapDepth() efficient for handling large or complex arrays?

It's efficient for moderate-sized arrays, but performance may vary with very large or deeply nested arrays, and careful testing is recommended.

Conclusion

Lodash's _.flatMapDepth() method is a flexible and powerful tool for mapping and flattening collections in JavaScript, especially when dealing with complex nested structures. It offers precise control over the depth of flattening, enabling tailored data manipulation suited to various programming needs.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass