Skip to main content

How to remove property / key from an object in nodejs

In Node.js, you can remove a property or key from an object using the `delete` keyword.

Here's an example:

const myObj = {
  name: 'John',
  age: 30,
  city: 'New York'


// Output: { name: 'John', age: 30 }

In this example, we create an object `myObj` with three properties: `name`, `age`, and `city`. We then use the `delete` keyword to remove the `city` property from the object. Finally, we log the object to the console to verify that the `city` property has been removed.

How to Check if Object is Empty in NodeJs?

In Node.js, you can check if an object is empty by using the Object.keys() method to get an array of the object's keys, and then checking the length of the array.

Here's an example:

const obj = {};

if (Object.keys(obj).length === 0) {
  console.log('Object is empty');
} else {
  console.log('Object is not empty');

In the above code, Object.keys(obj) returns an empty array because the obj object has no keys. So Object.keys(obj).length will be 0, indicating that the object is empty.