The lodash method _.map exported as a module for creating arrays of values by running each element through an iteratee function.
npx @tessl/cli install tessl/npm-lodash--map@4.6.0Lodash Map provides the _.map method as a standalone module for creating arrays of values by running each element in a collection through an iteratee function. It supports both arrays and objects, with multiple iteratee formats including functions, object shorthands, and property paths.
npm install lodash.mapvar map = require('lodash.map');For ES6 modules:
import map from 'lodash.map';var map = require('lodash.map');
// Basic array transformation with function
function square(n) {
return n * n;
}
map([4, 8], square);
// => [16, 64]
// Object transformation
map({ 'a': 4, 'b': 8 }, square);
// => [16, 64] (iteration order is not guaranteed)
// Property extraction using string shorthand
var users = [
{ 'user': 'barney' },
{ 'user': 'fred' }
];
map(users, 'user');
// => ['barney', 'fred']Creates an array of values by running each element in collection through iteratee. The iteratee is invoked with three arguments: (value, index|key, collection).
/**
* Creates an array of values by running each element in collection through iteratee
* @param {Array|Object} collection - The collection to iterate over
* @param {Function|Object|string} [iteratee=_.identity] - The function invoked per iteration
* @returns {Array} Returns the new mapped array
*/
function map(collection, iteratee);Parameters:
collection (Array|Object): The collection to iterate over. Can be an array, array-like object, or plain objectiteratee (Function|Object|string, optional): The function invoked per iteration. Defaults to identity function if not providedReturns:
Iteratee Function Signature: When using a function as iteratee, it receives three arguments:
value: The current element valueindex|key: The index of the element (for arrays) or property key (for objects)collection: The collection being iterated overIteratee Shorthand Options:
(value, index, collection) => transformedValue'user' extracts that property from each element{active: true} filters elements matching the objectUsage Examples:
var map = require('lodash.map');
// Function iteratee
function square(n) {
return n * n;
}
map([4, 8], square);
// => [16, 64]
// Object collection
map({ 'a': 4, 'b': 8 }, square);
// => [16, 64] (iteration order is not guaranteed)
// String property path shorthand
var users = [
{ 'user': 'barney' },
{ 'user': 'fred' }
];
map(users, 'user');
// => ['barney', 'fred']
// Object matching shorthand
var data = [
{ 'name': 'apple', 'organic': true },
{ 'name': 'banana', 'organic': false },
{ 'name': 'carrot', 'organic': true }
];
map(data, { 'organic': true });
// => [true, false, true]
// Identity function (shallow copy)
map([1, 2, 3]);
// => [1, 2, 3]
// Array-like objects
map('hello', function(char) { return char.toUpperCase(); });
// => ['H', 'E', 'L', 'L', 'O']Performance Notes:
arrayMap implementation for arraysbaseMap implementation for objects and array-like objectsgetIterateeCompatibility: