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
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]