The lodash method flatMap exported as a Node.js module for creating flattened arrays from mapped collections
npx @tessl/cli install tessl/npm-lodash--flatmap@4.5.0The lodash method flatMap exported as a Node.js module. Creates an array of flattened values by running each element in collection through iteratee and concating its result to the other mapped values. Part of lodash's modularized package series for selective imports.
npm install lodash.flatmapconst flatMap = require('lodash.flatmap');For ES modules:
import flatMap from 'lodash.flatmap';const flatMap = require('lodash.flatmap');
// Transform and flatten array elements
function duplicate(n) {
return [n, n];
}
const result = flatMap([1, 2], duplicate);
// => [1, 1, 2, 2]
// With objects
const users = [
{ name: 'alice', tags: ['admin', 'user'] },
{ name: 'bob', tags: ['user', 'guest'] }
];
const allTags = flatMap(users, user => user.tags);
// => ['admin', 'user', 'user', 'guest']Creates an array of flattened values by running each element in collection through iteratee and concating its result to the other mapped values.
/**
* Creates an array of flattened values by running each element in collection
* through iteratee and concating its result to the other mapped values.
* The iteratee is invoked with three arguments: (value, index|key, collection).
*
* @param {Array|Object} collection - The collection to iterate over
* @param {Function|string|Object} [iteratee=_.identity] - The function invoked per iteration
* @returns {Array} Returns the new flattened array
*/
function flatMap(collection, iteratee);Parameters:
{Array|Object} - The collection to iterate over. Can be arrays, array-like objects, or plain objects. Falsey values are treated as empty collections. Numeric values are treated as empty collections.{Function|string|Object} - The function invoked per iteration. Can be:
(value, index|key, collection) => any - Custom transformation function receiving three arguments'tags' to extract obj.tags)_.identity if not provided (returns the values as-is)Return Value:
ArrayIteratee Function Signature:
When providing a function as the iteratee:
/**
* @param {*} value - The current element being processed
* @param {number|string} index - The index of the element (for arrays) or key (for objects)
* @param {Array|Object} collection - The collection being iterated over
* @returns {*} The transformed value(s) to be flattened
*/
function iteratee(value, index, collection);Usage Examples:
const flatMap = require('lodash.flatmap');
// Basic array transformation
const numbers = flatMap([1, 2, 3], n => [n, n * 2]);
// => [1, 2, 2, 4, 3, 6]
// Object collection
const data = { a: [1, 2], b: [3, 4] };
const result = flatMap(data, arr => arr);
// => [1, 2, 3, 4]
// String property extraction
const objects = [
{ values: ['a', 'b'] },
{ values: ['c', 'd'] }
];
const extracted = flatMap(objects, 'values');
// => ['a', 'b', 'c', 'd']
// Object matching (iteratee as Object)
const users = [
{ name: 'alice', active: true, roles: ['admin'] },
{ name: 'bob', active: false, roles: ['user'] },
{ name: 'charlie', active: true, roles: ['user', 'moderator'] }
];
const activeUserRoles = flatMap(users, { active: true });
// => [{ name: 'alice', active: true, roles: ['admin'] }, { name: 'charlie', active: true, roles: ['user', 'moderator'] }]
// Empty collections and edge cases
const empty = flatMap([], n => [n, n]);
// => []
const nullish = flatMap(null);
// => []
const undefinedValue = flatMap(undefined);
// => []
const numeric = flatMap(1);
// => []
// Nested arrays (only flattens one level)
const nested = flatMap([[1, [2]], [3, [4]]], arr => arr);
// => [1, [2], 3, [4]]
// Object with length property (treated as object, not array-like)
const objWithLength = { length: [1, 2], other: [3, 4] };
const objResult = flatMap(objWithLength, val => val);
// => [1, 2, 3, 4]Error Handling:
null, undefined, and empty collections gracefully by returning empty arraysPerformance Characteristics: