Friday, July 24, 2026
HomeLanguagesJavascriptLodash _.flatMapDeep() Method

Lodash _.flatMapDeep() 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 _.flatMapDeep() method creates a flattened array of values by running each element in the given collection through the iteratee function and recursively flattens the mapped results. It is similar to _.flatMap() method.

Syntax:

_.flatMapDeep( collection, iteratee )

Parameters: This method accepts two 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.

Return Value: This method returns the new flattened array.

Example 1:




// Requiring the lodash library 
const _ = require("lodash");
      
// Original array 
var users = (['aaa', 'bbb', 'ccc', 
              'ddd', 'eee', 'fff']);
  
// Using the _.flatMapDeep() method
let flat_map =
  _.flatMapDeep(users,
    function duplicate(n) {
      return [[[n, n]]];
  }
)
  
// Printing the output 
console.log(flat_map);


Output:

[
  'aaa', 'aaa', 'bbb', 'bbb',
  'ccc', 'ccc', 'ddd', 'ddd',
  'eee', 'eee'
]

Example 2:




// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
var user1 = ([1, 2, 3, 4, 5, 6, 7]);
var user2 = (['a', 'b', 'c', 'd', 'e']);
  
// Using the _.flatMapDeep() method
let flat_map1 =
  _.flatMapDeep(user1,
    function makePattern(n) {
      return [[[n, n + "->"]]];
  }
)
   
let flat_map2 =
  _.flatMapDeep(user2,
    function makePattern(n) {
      return [[["<-" + n, n]]];
  }
)
  
// Printing the output 
console.log(flat_map1);
console.log(flat_map2);


Output:

[
  1, 1->, 2, 2->, 3, 3->,
  4, 4->, 5, 5->, 6, 6->,
  7, 7->
]
[
  '<-a', 'a', <-'b', 'b',
  '<-c', 'c', '<-d', 'd',
  '<-e', 'e'
]
RELATED ARTICLES

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6902 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6971 POSTS0 COMMENTS