Syntax, Parameter and Return Value
Syntax:
_.flatMap(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 flattened array.
Examples
Mapping and Flattening Numbers:
JavaScript
var _ = require('lodash');
var numbers = [1, 2, 3];
var doubledAndFlattened = _.flatMap(numbers, n => [n, n * 2]);
console.log(doubledAndFlattened);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 2, 2, 4, 3, 6]
This example demonstrates mapping numbers to their doubles and flattening the result.
Processing and Flattening Objects:
JavaScript
var users = [{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 }];
var namesAndAges = _.flatMap(users, u => [u.user, u.age]);
console.log(namesAndAges);

You can also try this code with Online Javascript Compiler
Run Code
Output:
['barney', 36, 'fred', 40]
Shows mapping an array of objects to an array of their properties and flattening it.
Nested Array Transformation:
JavaScript
var nestedArray = [[1, 2], [3, 4], [5, 6]];
var flatMapped = _.flatMap(nestedArray, x => x.map(n => n + 1));
console.log(flatMapped);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[2, 3, 4, 5, 6, 7]
An example of transforming and flattening a nested array.
Combining Arrays:
JavaScript
var arrays = [['a', 'b'], ['c', 'd']];
var combined = _.flatMap(arrays, x => x);
console.log(combined);

You can also try this code with Online Javascript Compiler
Run Code
Output:
['a', 'b', 'c', 'd']
Demonstrates the simple flattening of an array of arrays without transformation.
Frequently Asked Questions
How does _.flatMap() differ from _.map() followed by _.flatten()?
_.flatMap() combines mapping and flattening into one operation, offering a more concise and potentially more efficient approach than using _.map() followed by _.flatten() separately.
Can _.flatMap() handle deeply nested arrays?
_.flatMap() flattens only one level deep after mapping. For deeper flattening, consider using _.flatMapDeep().
Is _.flatMap() suitable for complex data transformations?
_.flatMap() is versatile and can handle complex transformations, but the clarity and performance should be considered for very complex or large datasets.
Conclusion
Lodash's _.flatMap() method is a practical and efficient tool for transforming and flattening collections in JavaScript. It elegantly simplifies the process of mapping each element in a collection and then flattening the result, enhancing code efficiency and readability. This method is particularly useful in scenarios involving nested arrays or when both transformation and flattening of data are required.
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.