_.map() in Lodash

_.map() in Lodash

A quick look at the _.map function in lodash with sample code

dfeverx's photo

Published on Sep 21, 2021

1 min read

Lodash is a JavaScript library that provides utility functions for common programming tasks using the functional programming paradigm

_.map() function return an array by iterating through the given array by running the given iteratee the basic syntax of the method is


const resultArray =_.map( InputArray, iteratee)

Now we can look at some sample code

Find cube of the array element

 const findCube = (number) => number * number * number;
  var array = [5, 6, 3];
  const resultArr = _.map(array, findCube);
// [Output] :[ 125, 216, 27 ]

Convert a array of title into Slug

    convert the title to lowercase and replce all white space with "-"
  const convertToSlug = (title) => title
  .replace(/\s/g, "-"); 

  var titles = ["Vs code is awesome", "TS is awesome"];

  //   convert this title into a slug
  const slugs = _.map(titles, convertToSlug);
// [Output] : [ 'vs-code-is-awesome', 'ts-is-awesome' ]
Share this