How to use 'lodash' Package for Array Manipulation in a JavaScript Project ?
Last Updated :
28 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 the drop method in Lodash ? The Drop method in Lodash removes elements from the beginning of arrays or characters from the start of strings. You can implement the lodash drop method as implemented in the below approaches. Table of Content Removing elements from the beginning of an array using dropRemoving string characters fro
2 min read
How to Find & Update Values in an Array of Objects using Lodash ? To find and update values in an array of objects using Lodash, we employ utility functions like find or findIndex to iterate over the elements. These functions facilitate targeted updates based on specific conditions, enhancing data manipulation capabilities.Table of ContentUsing find and assign Fun
4 min read
Lodash Features that are Available in JavaScript Lodash is a JavaScript utility library that provides a wide range of functions. It offers an efficient API for working with arrays, objects, strings, functions, etc. We'll explore a few key features that are available in Lodash and demonstrate how they can be implemented using native JavaScript, red
4 min read
How to Convert Object Containing Objects into Array of Objects using Lodash? Lodash is a JavaScript utility library that provides predefined functions to make code more readable and cleaner. These functions are optimized for performance, often being faster than native JavaScript methods for complex operations.We will learn how to convert an object containing objects into an
3 min read
How to use the Reverse method in Lodash ? The Reverse method in Lodash is the go-to function for quickly reversing arrays or strings, offering a convenient and efficient way to manipulate data structures in JavaScript. The below approaches will explain how you can use the reverse method in Lodash. Table of Content Using the reverse() method
2 min read