nodejs remove null from object

To remove null values from an object in Node.js, you can follow these steps:

  1. First, you need to iterate over each property of the object. You can achieve this by using a loop or the Object.keys() method.

  2. Inside the loop, you can check if the property value is null using a conditional statement. If it is null, you can delete the property from the object using the delete keyword.

  3. After deleting the null property, you can continue iterating over the remaining properties until all null values have been removed.

Here is an example code snippet that demonstrates how to remove null values from an object using Node.js:

function removeNullValues(obj) {
  Object.keys(obj).forEach((key) => {
    if (obj[key] === null) {
      delete obj[key];
  return obj;

// Example usage
const myObject = {
  name: "John",
  age: null,
  gender: "Male",
  address: null,

const cleanedObject = removeNullValues(myObject);

In this example, the removeNullValues() function takes an object as an argument and iterates over each property using Object.keys(). It checks if the property value is null and deletes the property using delete if it is. The function then returns the cleaned object.

When you run this code, the output will be:

{ name: 'John', gender: 'Male' }

This output shows that the null values from the myObject have been removed, and only the non-null properties remain in the cleaned object.