How to use compact Function In Lodash
In this approach, we are using the compact function from Lodash to remove null values from the array arr, resulting in a new array res containing only non-null elements.
Example: The below example uses a compact function to Remove null from an Array in Lodash.
const _ = require('lodash');
let arr = [1, null, 3, null, 5];
let res = _.compact(arr);
console.log(res);
Output:
[ 1, 3, 5 ]
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 Function
- Using filter Function
- Using reject Function
Run the below command to install Lodash:
npm i lodash