CARVIEW |
Select Language
HTTP/2 200
accept-ranges: bytes
age: 0
cache-control: public,max-age=0,must-revalidate
cache-status: "Netlify Edge"; fwd=miss
content-encoding: gzip
content-type: text/html; charset=UTF-8
date: Wed, 15 Oct 2025 04:04:12 GMT
etag: "ec1dcad1c19e601ca942c27db7551795-ssl-df"
server: Netlify
strict-transport-security: max-age=31536000
vary: Accept-Encoding
x-nf-request-id: 01K7K0MCT3Q2MAB59YKEGJCFXF
Remove null from an Array with Lodash - Mastering JS
Remove null from an Array with Lodash
Jun 26, 2022
To remove a null from an array, you should use lodash's filter
function.
It takes two arguments:
collection
: the object or array to iterate over.predicate
: the function invoked per iteration.
The filter()
function returns a new array containing all elements predicate
returned a truthy value for.
To remove null
, you can call filter()
with v => v !== null
as the predicate
.
const _ = require('lodash');
const arr = ['a', true, null, undefined, 42];
_.filter(arr, v => v !== null); // ['a', true, undefined, 42]
To remove null
using filter
, you can use the _.isNull
function as the predicate
.
Simply add a negate in front of the isNull
and all null
values will be filtered out.
const _ = require('lodash');
const array = ['a', true, null, undefined, 42]; // ['a', true, undefined, 42]
_.filter(array, el => !_.isNull(el));
Did you find this tutorial useful? Say thanks by starring our repo on GitHub!