Syntax, Parameter and Return Value
Syntax:
_.flatMapDeep(collection, [iteratee=_.identity])
Parameters:
-
collection (Array|Object): The collection to iterate over.
- [iteratee=_.identity] (Function): The function invoked per iteration.
Return Value:
(Array) - Returns the new deeply flattened array.
Examples
Deep Mapping and Flattening of Numbers:
JavaScript
var _ = require('lodash');
var numbers = [1, [2, [3, [4]]]];
var deeplyMapped = _.flatMapDeep(numbers, n => [n, n * 2]);
console.log(deeplyMapped);
You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 2, 2, 4, 3, 6, 4, 8]
This demonstrates deep mapping and flattening where each number is doubled.
Transforming Nested Arrays:
JavaScript
var nestedArray = [[1, 2], [[3, 4], [5, 6]]];
var flatMappedDeep = _.flatMapDeep(nestedArray, x => x.map(n => n + 1));
console.log(flatMappedDeep);
You can also try this code with Online Javascript Compiler
Run Code
Output:
[2, 3, 4, 5, 6, 7]
Shows deep mapping and flattening of a complex nested array structure.
Handling Nested Objects and Arrays:
JavaScript
var objects = [{ 'values': [1, [2]] }, { 'values': [[3], 4] }];
var valuesFlattened = _.flatMapDeep(objects, o => o.values);
console.log(valuesFlattened);
You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 2, 3, 4]
Demonstrates transforming and deeply flattening a mix of nested objects and arrays.
Complex Data Transformation:
JavaScript
var complexData = [1, [2, [3, [4, [5]]]]];
var transformed = _.flatMapDeep(complexData, n => (n % 2 === 0 ? [n, n * 2] : [n]));
console.log(transformed);
You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 2, 4, 3, 4, 8, 5]
An example of a more complex transformation and deep flattening.
Frequently Asked Questions
How deep does _.flatMapDeep() flatten the array?
_.flatMapDeep() flattens the array to the deepest level, turning any level of nested arrays into a single, flat array after mapping.
Can _.flatMapDeep() be used with non-array collections?
Yes, it works with both arrays and objects, but the most common and practical use case is with nested arrays.
Is there a performance concern with _.flatMapDeep() on large datasets?
For very large or deeply nested datasets, there could be performance implications, so it's advisable to test and optimize as needed.
Conclusion
Lodash's _.flatMapDeep() method is a powerful tool for dealing with complex, nested array structures in JavaScript. It efficiently combines mapping and deep flattening, streamlining the process of transforming and reducing nested data into a manageable form. This method exemplifies Lodash's capability to simplify advanced data manipulation tasks.
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.