The modern build of lodash's flattenDeep utility function that recursively flattens nested arrays to any depth.
npx @tessl/cli install tessl/npm-lodash--flattendeep@3.0.0lodash.flattendeep provides a standalone implementation of lodash's flattenDeep utility function that recursively flattens nested arrays to any depth. It offers a simple API that takes an array as input and returns a new flattened array, making it ideal for data processing scenarios where nested array structures need to be simplified.
npm install lodash.flattendeepvar flattenDeep = require('lodash.flattendeep');For ES modules:
import flattenDeep from 'lodash.flattendeep';var flattenDeep = require('lodash.flattendeep');
// Flatten a deeply nested array
var nestedArray = [1, [2, 3, [4]]];
var result = flattenDeep(nestedArray);
console.log(result);
// => [1, 2, 3, 4]
// Works with any depth of nesting
var deeplyNested = [1, [2, [3, [4, [5]]]]];
var flattened = flattenDeep(deeplyNested);
console.log(flattened);
// => [1, 2, 3, 4, 5]
// Handles empty arrays and mixed types
var mixedArray = [1, [], [2, [3, []]], 4];
var mixedResult = flattenDeep(mixedArray);
console.log(mixedResult);
// => [1, 2, 3, 4]Recursively flattens a nested array to any depth, returning a new flat array with all nested elements.
/**
* Recursively flattens a nested array.
*
* @param {Array} array The array to recursively flatten.
* @returns {Array} Returns the new flattened array.
*/
function flattenDeep(array);Parameters:
array (Array): The array to recursively flatten. Can be null, undefined, or an empty array.Returns:
Behavior:
null and undefined inputs by returning an empty arrayUsage Examples:
var flattenDeep = require('lodash.flattendeep');
// Basic nested array
flattenDeep([1, [2, 3, [4]]]);
// => [1, 2, 3, 4]
// Deep nesting
flattenDeep([1, [2, [3, [4, [5, 6]]]]]);
// => [1, 2, 3, 4, 5, 6]
// Mixed types
flattenDeep([1, ['a', [true, [null]]]]);
// => [1, 'a', true, null]
// Empty arrays
flattenDeep([[], [1, []], [2]]);
// => [1, 2]
// Null/undefined input
flattenDeep(null);
// => []
flattenDeep(undefined);
// => []
// Empty array
flattenDeep([]);
// => []