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
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
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
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
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 DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.