Skip to content
the poor coder

nodejs remove null from object

Poor Man
Published
Reading time
2 min read
On this page

Node.js: Removing Null from Object#

As a developer, you may need to remove any null properties from an object in Node.js. This can be achieved easily with built-in functions of JavaScript.

Method 1: Using delete operator#

You can use the delete operator to remove the null values from an object. This method will delete the property from the object if it has a value of null or undefined. Here's an example:

Text

            let obj = {
                name: "John",
                age: null,
                address: {
                    city: "New York",
                    state: null,
                    country: "USA"
                }
            };

            for (let prop in obj) {
                if (obj[prop] == null) {
                    delete obj[prop];
                } else if (typeof obj[prop] === "object") {
                    for (let nestedProp in obj[prop]) {
                        if (obj[prop][nestedProp] == null) {
                            delete obj[prop][nestedProp];
                        }
                    }
                }
            }

            console.log(obj);
        

In this example, we are using a for loop to iterate over each property of the object. If a property has a value of null, we are deleting that property using the delete operator. If a property is an object, we use another for loop to iterate over its nested properties and delete any null values.

Method 2: Using Object.entries() and reduce()#

You can also use the Object.entries() method to convert the object into an array of key-value pairs and then use the reduce() method to create a new object without null values. Here's an example:

Text

            let obj = {
                name: "John",
                age: null,
                address: {
                    city: "New York",
                    state: null,
                    country: "USA"
                }
            };

            let newObj = Object.entries(obj).reduce((acc, [key, value]) => {
                if (value !== null) {
                    if (typeof value === "object") {
                        acc[key] = removeNull(value);
                    } else {
                        acc[key] = value;
                    }
                }

                return acc;
            }, {});

            console.log(newObj);
        

In this example, we are using Object.entries() to convert the object into an array of key-value pairs. Then we use the reduce() method to iterate over each key-value pair and create a new object without null values. If a value is an object, we recursively call the function to remove null values from nested objects.

Both methods are effective in removing null values from an object in Node.js. Choose the one that suits your needs best.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC