How to use 'lodash' Package for Array Manipulation in a JavaScript Project ?
Last Updated :
24 Apr, 2025
In this article, we'll look at how to use lodash for array manipulation tasks like sorting, filtering, mapping, etc. The 'lodash' is a very important and useful npm package that provides a set of functions to work with arrays, strings, objects, etc. Working with arrays is an important task in a javascript project which can be done through the 'lodash' functions.
How to use 'lodash' Package for Array Manipulation in a JavaScript Project:
Installation: Before using this package, we have to install it first. Follow the given steps to install it.
- Open the terminal and navigate to the same directory of the project.
- Enter the given command to install the module.
- After successful installation, a JSON file is added to the directory that contains all details about the installed modules or packages.
$ npm install lodash
installation
Import the package: After installation, we can use this package in our project or JavaScript application. For using this, we have to import the installed package. Follow the given steps for this :
- We have to use require function to import the installed module.
- Pass the name of the module as an argument to require function.
- After successful installation and importing of the module, we can use it in our project or application.
const lodash = require('lodash');
Run the code:
- After successfully importing and writing the code, we have to run our javascript code file using the command shown below.
- Here, index.js is the name of the file.
$ node index.js
Approaches for array manipulation:
Sorting: Sorting is a common task in a javascript project which can be done by the available functions in lodash package. There is a function sortBy() which can sort the array and arrange it in ascending order.
Example: In this example, we have imported the 'lodash' library using the require function and assign it to the lodash variable and we have created an array of numbers called numbers. This array contains 10 numbers. We then use the lodash.sortBy method to sort the numbers array in ascending order. This means that the smallest number will come first, followed by the second smallest number, and so on, until we reach the largest number. Finally, we log the sorted array to the console using the console.log function.
JavaScript
const lodash = require('lodash');
const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3];
// Sort the numbers array in ascending order
const sortedNumbers = lodash.sortBy(numbers);
console.log(sortedNumbers);
Output:[1, 1, 2, 3, 3, 4, 5, 5, 6, 9]
Filtering: There are many functions available in 'lodash' package used for filtering purposes. Here's an example of how to filter an array of numbers using the filter function:
Example: Firstly, the code imports the 'lodash' library using the require() function and assigns it to a variable called lodash. Then, an array of numbers is created and assigned to the variable numbers. The lodash.filter() function is then used to filter the numbers array based on a condition. In this case, the condition is that the number must be even and the filtered numbers are then assigned to a new variable called filteredNumbers. Finally, the console.log() function is used to print the filteredNumbers array to the console.
JavaScript
const lodash= require('lodash');
const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3];
// Filtering
const filteredNumbers = lodash.filter(
numbers, n => n % 2 === 0);
console.log(filteredNumbers);
Output: [4 ,2, 6]
Mapping: In mapping, the map() function is used to map the array of numbers. An example of this is given below:
Example: In this example, firstly we have imported the module 'lodash' using the require function and then we have created an array of 10 numbers named numbers. Then we used the map method to map the number according to the condition given in the method. And at last, we have displayed the output.
JavaScript
const lodash = require('lodash');
const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3];
const squaredNumbers = lodash.map(numbers, n => n * n);
console.log(squaredNumbers);
Output:[9, 1, 16, 1, 25, 81, 4, 36, 25, 9]
Similar Reads
How to Split JavaScript Array in Chunks using Lodash?
Splitting a JavaScript array into chunks consists of dividing the array into smaller subarrays based on a specified size or condition. In this article, we will explore different approaches to splitting JavaScript array into chunks using Lodash. Below are the approaches to Split JavaScript array in c
2 min read
Split JavaScript Array in Chunks Using Lodash?
When working with arrays in JavaScript, you may encounter situations where you need to split an array into smaller chunks. This can be particularly useful when dealing with large datasets or when performing operations in a more manageable and efficient manner. The Lodash library provides a convenien
2 min read
How to Remove Null from an Array in Lodash ?
Removing null values from Array is important because it improves data cleanliness and ensures accurate processing. Below are the approaches to Remove null from an Array in Lodash: Table of Content Using compact FunctionUsing filter FunctionUsing reject FunctionRun the below command to install Lodash
2 min read
How to Import a Single Lodash Function in JavaScript ?
In JavaScript, we have to import various functions for operation, but importing a single function is also important for optimizing code and reducing unnecessary dependencies. Below are the approaches to importing a single Lodash function in JavaScript: Table of Content Using ES6 import syntaxUsing C
2 min read
How to Convert Object to Array in Lodash ?
Converting an Object to an Array consists of changing the data structure from key-value pairs to an array format. Below are the different approaches to converting objects to arrays in Lodash: Table of Content Using toArray function Using values functionRun the below command before running the below
2 min read
How to use Lodash to Find & Return an Object from Array ?
JavaScript's built-in array methods offer basic functionality whereas Lodash, a popular utility library, empowers developers with robust tools for complex array operations. Below are the methods to find and return an object from array using Lodash: Table of Content Using _.find()Using _.findIndex()
3 min read
How to use Lodash to find & Return an Object from Array ?
In JavaScript, the Lodash Module has different methods of doing and get objects from the array. we will explore three different methods with practical implementation of each approach in terms of examples and output to to find and return an object from Array. These are the following methods: Table of
3 min read
How to Convert Object Array to Hash Map using Lodash ?
Converting an Object Array to a Hash Map consists of manipulating the array elements to create a key-value mapping. Below are the different approaches to Converting an object array to a hash map using Lodash:Table of Content Using keyBy() functionUsing reduce functionRun the below command before run
2 min read
How to Replace Objects in Array using Lodash?
Replacing objects in an array using Lodash typically involves identifying the specific object(s) you want to replace and then performing the replacement operation. Lodash provides a range of methods to facilitate such operations, although the actual replacement might involve combining Lodash functio
3 min read
How to Remove Array Items Recursively in Lodash?
Sometimes, we need to remove items based on a specific condition. Sometimes, these arrays can be nested, meaning they contain other arrays within them. In such cases, we need to remove items not just from the main array but from all levels within it. This process is known as "recursive removal." we
2 min read