Last updated: Mar 1, 2024
Reading timeยท4 min

null and undefined values from a nested objectnull and undefined values from an object using reduce()null and undefined values from an object using a for...in loopTo remove all null values from an object:
Object.keys() method to get an array of the object's keys.forEach() method to iterate over the array.null.null values using the delete operator.// โ Remove null values from an object const obj = { one: null, two: 2, three: null, }; Object.keys(obj).forEach(key => { if (obj[key] === null) { delete obj[key]; } }); console.log(obj); // ๐๏ธ {two: 2}

If you need to remove all null and undefined values from the object, use the
loose equality (==) operator.
// โ Remove null and undefined values from an object const obj = { a: null, b: undefined, c: 'test', }; Object.keys(obj).forEach(key => { if (obj[key] == null) { delete obj[key]; } }); console.log(obj); // ๐๏ธ {c: 'test'}

The code sample above removes the null and undefined values from the object.
If you only need to remove the undefined values from the object, use
undefined in the place of null.
// โ Remove undefined values from an object const obj = { a: undefined, b: 'test', c: undefined, }; Object.keys(obj).forEach(key => { if (obj[key] === undefined) { delete obj[key]; } }); console.log(obj); // ๐๏ธ {b: 'test'}

The Object.keys() method returns an array containing the object's keys.
const obj = { one: null, two: 2, three: null, }; // ๐๏ธ ['one', 'two', 'three'] console.log(Object.keys(obj));
The Array.forEach() method allows us to iterate over the array of keys.
The function we passed to the forEach() method gets called with each element
(key) in the array.
const obj = { one: null, two: 2, three: null, }; Object.keys(obj).forEach(key => { if (obj[key] === null) { delete obj[key]; } }); console.log(obj); // ๐๏ธ {two: 2}
We check if the value, associated with the current key is equal to null.
If the condition is met, we use the delete operator to delete the key-value pair.
If you need to remove the null values from a nested object, use a recursive
function.
null and undefined values from a nested objectTo remove the null and undefined values from a nested object:
Object.entries() method to get a two-dimensional array of key-value
pairs.filter() method to remove all null or undefined values from the
array.null or undefined
values.// โ Remove all `null` and `undefined` values from a nested object const obj = { one: null, two: 2, three: { four: 4, five: null, six: undefined, }, }; function removeNull(obj) { return Object.fromEntries( Object.entries(obj) .filter(([_, value]) => value != null) .map(([key, value]) => [ key, value === Object(value) ? removeNull(value) : value, ]), ); } const result = removeNull(obj); // ๐๏ธ { two: 2, three: { four: 4 } } console.log(result);

Notice that we used loose not equals (!=) to remove both null and
undefined values from the nested object.
The loose equality operators (== and !=) cover both null and undefined
because null and undefined are equal when compared loosely.
console.log(null == undefined); // ๐๏ธ true console.log(null === undefined); // ๐๏ธ false
The Object.entries() method returns a two-dimensional array of the object's
key-value pairs.
const obj = { one: null, two: 2, three: { four: 4, five: null, six: undefined, }, }; // [ // [ 'one', null ], // [ 'two', 2 ], // [ 'three', { four: 4, five: null, six: undefined } ] // ] console.log(Object.entries(obj));
We used the Array.filter() method to
remove the null values from the array
and then used the Array.map() method to call the removeNull() function with
the value if the value is an object.
The removeNull() function removes all null values from the nested object.
Alternatively, you can use the Array.reduce() method.
null and undefined values from an object using reduce()This is a three-step process:
Array.reduce().null or undefined.accumulator
variable.const obj = { one: null, two: 2, three: null, four: null, five: 5, six: undefined, }; const newObj = Object.keys(obj).reduce((accumulator, key) => { if (obj[key] != null) { accumulator[key] = obj[key]; } return accumulator; }, {}); // ๐๏ธ { two: 2, five: 5 } console.log(newObj);
We used the Object.keys() method to get an array of the object's keys and used
the Array.reduce() method to iterate over the array.
Array.reduce() is an empty object and serves as the initial value for the accumulator variable.On each iteration, we check if the current key doesn't have a value of null or
undefined.
If the condition is met, we assign the key-value pair to the accumulator
variable.
Alternatively, you can use a for...in loop.
null and undefined values from an object using a for...in loopThis is a three-step process:
for...in loop to iterate over the object.null or undefined.delete operator to delete each null or undefined value.const obj = { one: null, two: 2, three: null, four: undefined, }; for (const key in obj) { if (obj[key] == null) { delete obj[key]; } } console.log(obj); // ๐๏ธ {two: 2}
The for...in loop allows us to iterate over the object's properties.
We check if the value of the current property is equal to null or undefined.
If the condition is met, we delete the key-value pair using the delete
operator.
Which approach you pick is a matter of personal preference. I'd use the
Object.keys() and Array.forEach() methods as I find the approach quite
direct and intuitive.
You can learn more about the related topics by checking out the following tutorials: