Lodash _.flatMapDepth() Method
Last Updated :
14 Sep, 2020
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 _.flatMapDepth() method creates a flattened array of values by running each element in the given collection through the iteratee function. It recursively flattens the mapped results up to the given depth value. It is similar to the _.flatMap() method.
Syntax:
_.flatMapDepth( collection, iteratee, depth )
Parameters: This method accepts three 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.
- depth: It is a number that specifies the maximum recursion depth. It is an optional parameter. The default value is 1.
Return Value: This method returns the new flattened array.
Example 1:
const _ = require( "lodash" );
var users = ([3, 4]);
let flat_map = _.flatMapDepth(users, duplicate, 2 )
function duplicate(n) {
return [[[n, n]]];
}
console.log(flat_map);
|
Output:
[ [ 3, 3 ], [ 4, 4 ] ]
Example 2:
const _ = require( "lodash" );
var users = ([ 'q' , 'r' , 't' , 'u' ]);
let flat_map = _.flatMapDepth(users, duplicate, 2 )
function duplicate(n) {
return [[[n, n]]];
}
console.log(flat_map);
|
Output:
[ [ 'q', 'q' ], [ 'r', 'r' ], [ 't', 't' ], ['u', 'u' ] ]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...