add after ary assign assignIn assignInWith assignWith at before bind bindAll bindKey camelCase capitalize castArray ceil chain chunk clamp clone cloneDeep cloneDeepWith cloneWith compact concat conformsTo countBy create curry curryRight debounce deburr defaults defaultsDeep defer delay difference divide drop dropRight each eachRight endsWith entries entriesIn eq escape every extend extendWith fill filter find findIndex findKey findLast findLastIndex findLastKey first flatMap flatMapDeep flatMapDepth flatten flattenDeep flattenDepth flip floor forEach forEachRight forIn forInRight forOwn forOwnRight fromPairs functions functionsIn get groupBy gt gte has hasIn head inRange includes indexOf initial intersection invert invertBy invoke invokeMap isArguments isArray isArrayBuffer isArrayLike isArrayLikeObject isBoolean isBuffer isDate isElement isEmpty isEqual isEqualWith isError isFinite isFunction isInteger isLength isMap isMatch isMatchWith isNaN isNative isNil isNull isNumber isObject isObjectLike isPlainObject isRegExp isSafeInteger isSet isString isSymbol isTypedArray isUndefined isWeakMap isWeakSet join kebabCase keyBy keys keysIn last lastIndexOf lowerCase lowerFirst lt lte map mapKeys mapValues max maxBy mean meanBy memoize merge mergeWith min minBy multiply negate nth omit omitBy once orderBy overArgs pad padEnd padStart parseInt partial partialRight partition pick pickBy pull pullAll pullAllBy pullAllWith pullAt random rearg reduce reduceRight reject remove repeat replace rest result reverse round sample sampleSize set setWith shuffle size slice snakeCase some sortBy sortedIndex sortedIndexOf sortedLastIndex sortedLastIndexOf sortedUniq split spread startCase startsWith subtract sum sumBy tail take takeRight tap template throttle thru toArray toFinite toInteger toLength toLower toNumber toPairs toPairsIn toPlainObject toSafeInteger toString toUpper transform trim trimEnd trimStart truncate unary unescape union uniq uniqBy uniqWith unset unzip unzipWith update updateWith upperCase upperFirst values valuesIn without words wrap wrapperAt wrapperChain wrapperCommit wrapperFlatMap wrapperLodash wrapperNext wrapperPlant wrapperReverse wrapperToIterator wrapperValue xor zip zipObject zipObjectDeep zipWith

Lodash _.flatMapDepth

Overview

Overview of Lodash _.flatMapDepth Method

Lodash is a JavaScript library that provides a wide range of utility functions for common programming tasks. One of its methods, _.flatMapDepth, is used to flatten a nested array to a specified depth and apply a function to each element of the array.

Definition

The _.flatMapDepth method is used to recursively flatten a nested array to a specified depth, while applying a function to each element of the array. Its signature is as follows:

.flatMapDepth(collection, [iteratee=.identity], [depth=1])

Parameters

  • collection (Array|Object): The collection to iterate over.
  • iteratee (Function): The function to invoke for each element.
  • depth (number): The maximum recursion depth.

Returns

(array): Returns the new flattened array.

Syntax

_.flatMapDepth(array, [iteratee = _.identity], [depth = 1]);

Examples

Here are a few examples to help you understand how to use the _.flatMapDepth method:

Example 1: Flatten a nested array to a depth of 3

const array = [1, [2, [3, [4]], 5]]; _.flatMapDepth(array, (value) => value, 3); // Output: [1, 2, 3, 4, 5]

In this example, we use _.flatMapDepth to flatten the nested array to a depth of 3, and apply the _.identity function to each value. The flattened array [1, 2, 3, 4, 5] is returned.

Example 2: Flatten a nested array with a custom function

const array = [[1, 2], [3, 4], [5, 6]]; _.flatMapDepth(array, (array) => array.map((value) => value * 2), 1); // Output: [2, 4, 6, 8, 10, 12]

In this example, we use _.flatMapDepth to flatten the nested array to a depth of 1, and apply a custom function that doubles each value of the array. The flattened array [2, 4, 6, 8, 10, 12] is returned.

Example 3: Flatten a nested object to a depth of 2

const object = { a: { b: { c: [1, 2, 3] }, d: [4, 5] }, e: { f: [6, 7], g: { h: [8] } }, }; _.flatMapDepth(object, (value) => value, 2); // Output: [1, 2, 3, 4, 5, 6, 7, { h: [ 8 ] }]

In this example, we use _.flatMapDepth to flatten the nested object to a depth of 2, and apply the _.identity function to each value. The flattened array [1, 2, 3, 4, 5, 6, 7, { h: [ 8 ] }] is returned.

Conclusion

The _.flatMapDepth method is a useful utility function for flattening nested arrays or objects to a specified depth, while applying a custom function to each element. Its flexibility makes it a valuable tool for building complex data processing pipelines in JavaScript.