Navigate back to the homepage

How to remove keys with null values in javascript?

Infinitbility
JavaScript
January 30th, 2022 · 1 min read
keep affilitate

Hello Friends 👋,

Welcome to Infinitbility.

When we got objects from the server or API sometime’s we get keys with null or undefined values and it will throw an error when we use them blindly.

I.e we will learn how to delete keys that have null, or undefined values.

To remove javascript falsy values we used javascript Object.keys(), forEach() or for loop, and delete operator.

  • javascript Object.keys() method to get an array of the object’s keys.
  • forEach() or for loop to check every keys in object.
  • delete operator to remove key values from the object.

JavaScript forEach loop example to remove null, or undefined values

1const obj = {
2 id: 1,
3 name: "infinitbility",
4 age: null,
5 waight: undefined,
6 isActive: false,
7};
8
9Object.keys(obj).forEach(key => {
10 if (obj[key] === null || obj[key] === undefined) {
11 delete obj[key];
12 }
13});
14
15console.log(obj); // { id: 1, name: 'infinitbility', isActive: false }

Output

JavaScript, forEach, remove, null, undefined, example
JavaScript forEach example to remove null, or undefined values

JavaScript for loop example to remove null, or undefined values

Using for loop we will bypass the Object.keys() method and let’s take an example.

1const obj = {
2 id: 1,
3 name: "infinitbility",
4 age: null,
5 waight: undefined,
6 isActive: false,
7};
8
9for (const key in obj) {
10 if (obj[key] === null || obj[key] === undefined) {
11 delete obj[key];
12 }
13}
14
15console.log(obj); // { id: 1, name: 'infinitbility', isActive: false }

Output

JavaScript, forEach, remove, null, undefined, example
JavaScript for example to remove null, or undefined values

Thanks for reading…

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

melio affilitate

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to convert number to decimal in typescript?

TypeScript, convert number to decimal example.

January 29th, 2022 · 1 min read

How to convert string array to number array in typescript?

TypeScript, convert string arrays to number arrays example.

January 28th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]