What is lodash.map?
The lodash.map package is a utility library that provides a method for creating a new array populated with the results of calling a provided function on every element in the calling array. It is part of the larger Lodash library, which offers a wide range of utility functions for common programming tasks.
What are lodash.map's main functionalities?
Basic Mapping
This feature allows you to apply a function to each element in an array and create a new array with the results. In this example, each number in the array is multiplied by 2.
const _ = require('lodash.map');
const array = [1, 2, 3, 4];
const result = _.map(array, function(n) { return n * 2; });
console.log(result); // [2, 4, 6, 8]
Mapping with Objects
This feature allows you to map over the values of an object and create a new array with the results. In this example, the ages of the users are extracted into a new array.
const _ = require('lodash.map');
const users = { 'fred': { 'user': 'fred', 'age': 40 }, 'pebbles': { 'user': 'pebbles', 'age': 1 } };
const result = _.map(users, function(user) { return user.age; });
console.log(result); // [40, 1]
Mapping with Index
This feature allows you to use the index of each element in the array during the mapping process. In this example, each number is multiplied by its index.
const _ = require('lodash.map');
const array = [1, 2, 3, 4];
const result = _.map(array, function(n, index) { return n * index; });
console.log(result); // [0, 2, 6, 12]
Other packages similar to lodash.map
underscore
Underscore is a JavaScript library that provides utility functions for common programming tasks. It includes a map function similar to lodash.map, but Lodash is generally considered to be faster and more modular.
ramda
Ramda is a functional programming library for JavaScript. It provides a map function that is similar to lodash.map but emphasizes immutability and function composition.
array-map
array-map is a lightweight package that provides a map function for arrays. It is less feature-rich compared to lodash.map but can be a good choice for simple use cases where a full utility library is not needed.
lodash.map v2.0.0
The Lo-Dash function _.map
as a Node.js module generated by lodash-cli.
Dive in
There’s plenty of documentation, unit tests, & benchmarks.
Support
lodash.map has been tested in at least Node.js 0.6.8-0.10.18.
Author
Contributors