CARVIEW |
Select Language
HTTP/2 200
accept-ranges: bytes
age: 2
cache-control: public,max-age=0,must-revalidate
cache-status: "Netlify Edge"; fwd=miss
content-encoding: gzip
content-type: text/html; charset=UTF-8
date: Thu, 16 Oct 2025 04:02:32 GMT
etag: "549e1e33b74dcd5cdab53c14fb011def-ssl-df"
server: Netlify
strict-transport-security: max-age=31536000
vary: Accept-Encoding
x-nf-request-id: 01K7NJY1MMAV9E7BM9NCK1TQ1M
Lodash's `map()` Function - Mastering JS
Lodash's `map()` Function
Apr 8, 2020
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
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
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]
Did you find this tutorial useful? Say thanks by starring our repo on GitHub!