Lodash’s `map()` Function
In this post, we will give you information about Lodash’s `map()` Function. Here we will give you detail about Lodash’s `map()` Function And how to use it also give you a demo for it if it is necessary.
Given an array arr
and a function fn
, Lodash’s map()
function returns an array containing the return values of fn()
on every element in the array.
const arr = [1, 2, 3, 4];
_.map(arr, v => v * 2); // [2, 4, 6, 8]
On Arrays of Objects for Lodash’s `map()` Function
Given an array of objects, you can pass a string as fn
instead of a function to get array containing each object’s value for the property fn
.
const arr = [
{ firstName: 'Will', lastName: 'Riker', rank: 'Commander' },
{ firstName: 'Beverly', lastName: 'Crusher', rank: 'Commander' },
{ firstName: 'Wesley', lastName: 'Crusher', rank: 'Ensign' }
];
_.map(arr, 'firstName'); // ['Will', 'Beverly', 'Wesley']
// Equivalent:
_.map(arr, v => v.firstName); // ['Will', 'Beverly', 'Wesley']
On Objects for Lodash’s `map()` Function
You can also call _.map()
on an object. _.map(obj, fn)
behaves like _.map(Object.values(obj), fn)
.
const obj = {
one: 1,
two: 2,
three: 3,
four: 4
};
_.map(obj, v => v * 2); // [2, 4, 6, 8]
Lodash is a JavaScript library which provides utility functions for common programming tasks using the functional programming paradigm. Lodash is a JavaScript library that helps programmers write more concise and maintainable JavaScript. It can be broken down into several main areas: Utilities: for simplifying common programming tasks such as determining type as well as simplifying math operations.
Why Lodash?
Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc.Lodash’s modular methods are great for:
- Iterating arrays, objects, & strings
- Manipulating & testing values
- Creating composite functions
Hope this code and post will helped you for implement Lodash’s `map()` Function. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs