Installation
To use Lodash in your project, you need to install it first. You can do this either via npm or by downloading the standalone library from the Lodash website.
To install Lodash via npm, open your command line interface and run the following command:
npm install lodash
Basic usage
To use Lodash in your code, you need to include it in your JavaScript file by requiring it:
const _ = require('lodash');
Arrays
Lodash provides a variety of functions for working with arrays. Here are some of the most commonly used:
_.map()
_.map(array, iteratee)
The
_.map() function applies a function to each element in an array and returns a new array with the results. The first argument is the array, and the second argument is the function to apply.const numbers = [1, 2, 3]; const squares = _.map(numbers, (n) => n * n); console.log(squares); // [1, 4, 9]
_.filter()
_.filter(array, predicate)
The
_.filter() function creates a new array with all elements that pass the test implemented by the provided function. The first argument is the array, and the second argument is the function to apply.const numbers = [1, 2, 3, 4, 5]; const evenNumbers = _.filter(numbers, (n) => n % 2 === 0); console.log(evenNumbers); // [2, 4]
_.reduce()
_.reduce(array, iteratee, [accumulator])
The
_.reduce() function reduces an array to a single value by applying a function to each element of the array. The first argument is the array, the second argument is the function to apply, and the third argument (optional) is the initial value of the accumulator.const numbers = [1, 2, 3, 4, 5]; const sum = _.reduce(numbers, (accumulator, n) => accumulator + n, 0); console.log(sum); // 15
_.orderBy()
_.orderBy(array, [iteratees=[_.identity]], [orders])
The
_.orderBy() function creates a new sorted array of objects ordered by properties and orders. The first argument is the array to sort, the second argument (optional) is the property to sort by, and the third argument (optional) is the order to sort in.const users = [ { name: 'John', age: 25 }, { name: 'Jane', age: 20 }, { name: 'Bob', age: 30 } ]; const sortedUsers = _.orderBy(users, ['name'], ['asc']); console.log(sortedUsers); // [{ name: 'Bob', age: 30 }, { name: 'Jane', age: 20 }, { name: 'John', age: 25 }]
Here is a list of some of the most commonly used functions in Lodash and a brief explanation of what they do:
Arrays
_.chunk(array, size)- Creates an array of elements split into groups the length ofsize.
_.compact(array)- Creates an array with all falsey values removed. Falsey values arefalse,null,0,"",undefined, andNaN.
_.concat(array, [values])- Creates a new array concatenatingarraywith any additional arrays and/or values.
_.difference(array, [values])- Creates an array of unique values not included in the other given arrays.
_.drop(array, [n=1])- Creates a slice ofarraywithnelements dropped from the beginning.
_.dropRight(array, [n=1])- Creates a slice ofarraywithnelements dropped from the end.
_.fill(array, value, [start=0], [end=array.length])- Fills elements ofarraywithvaluefromstartup to, but not including,end.
_.find(array, [predicate=_.identity], [fromIndex=0])- Iterates over elements ofarray, returning the first element that the predicate returns truthy for.
_.flatten(array)- Flattensarraya single level deep.
_.intersection([arrays])- Creates an array of unique values that are included in all given arrays.
_.join(array, [separator=','])- Converts all elements inarrayinto a string separated byseparator.
_.map(collection, [iteratee=_.identity])- Creates an array of values by running each element ofcollectionthroughiteratee.
_.orderBy(collection, [iteratees=[_.identity]], [orders])- Sortscollectionbyiterateesandorders.
_.reduce(collection, [iteratee=_.identity], [accumulator])- Reducescollectionto a value which is the accumulated result of running each element incollectionthroughiteratee.
_.reverse(array)- Reversesarray.
_.slice(array, [start=0], [end=array.length])- Creates a slice ofarrayfromstartup to, but not including,end.
_.sortBy(collection, [iteratees=[_.identity]])- Sortscollectionbyiteratees.
Objects
_.assign(object, [sources])- Assigns own enumerable string keyed properties ofsourceobjects to the destination object.
_.clone(value)- Creates a shallow clone ofvalue.
_.defaults(object, [sources])- Assigns own enumerable string keyed properties ofsourceobjects to the destination object for all destination properties that resolve to undefined.
_.findKey(object, [predicate=_.identity])- Iterates over own enumerable properties ofobject, returning the key that the predicate returns truthy for.
_.get(object, path, [defaultValue])- Gets the value atpathofobject. If the resolved value isundefined, thedefaultValueis returned in its place.
_.has(object, path)- Checks ifpathis a direct property ofobject.
_.invert(object)- Creates an object composed of the inverted keys and values ofobject.
_.keys(object)- Creates an array of the own enumerable property names of `object

