js remove nested property from object

"Remove Nested Props from JS Objects"

By Rachel Jackson
AD

Ever found yourself dealing with a nested property in an object that you simply don't need or want anymore? As a JavaScript developer, you're aware that JS objects are mutable, meaning you can remove or modify properties as needed. However, when the property you want to remove is nested, the task becomes a bit more complex. Let's dive into how you can remove nested properties from objects in JavaScript.

Before we get started, it's important to understand that removing a nested property involves two steps. First, you need to navigate to the property you want to remove. Then, you can delete it. This process can be a bit trickier than it sounds, especially when dealing with dynamic properties or arrays. Let's break it down.

Using Dot Notation to Delete Nested Properties

The first and most straightforward method to remove a nested property is by using dot notation. Dot notation allows you to access or modify an object's properties by using a periodic series of keys. For example, consider the following nested object:

AD
Merge nested IF
Merge nested IF
an image of a computer screen with the text'document object model'on it
an image of a computer screen with the text'document object model'on it
web development programming coding
web development programming coding

let obj = {
  foo: {
    bar: 'baz'
  }
};

JavaScript’s Tantrum: Fixing “Object Is Not Iterable (Cannot Read Property Symbol(Symbol.iterator))” Without Pulling Your Hair Out
JavaScript’s Tantrum: Fixing “Object Is Not Iterable (Cannot Read Property Symbol(Symbol.iterator))” Without Pulling Your Hair Out
How to Safely Access Deeply Nested Properties in JavaScript
How to Safely Access Deeply Nested Properties in JavaScript
What Is Closure In Javascript, Javascript Closure Example, How To Use Closures In Javascript, Javascript Closure Explanation, Javascript Practice Questions, Choosing Between Java And Javascript, Javascript Programming Concepts, Recursive Javascript Function, Javascript Apply Call Bind Difference
What Is Closure In Javascript, Javascript Closure Example, How To Use Closures In Javascript, Javascript Closure Explanation, Javascript Practice Questions, Choosing Between Java And Javascript, Javascript Programming Concepts, Recursive Javascript Function, Javascript Apply Call Bind Difference

Removing a Directly Nested Property

To remove the 'bar' property from the 'foo' object, you can simply use the following code:

AD
delete obj.foo.bar;

JavaScript Objects: The Key to Structured Data 🚀
JavaScript Objects: The Key to Structured Data 🚀
the instructions for how to use an object in visual language, including text and numbers
the instructions for how to use an object in visual language, including text and numbers
js remove nested property from object
js remove nested property from object
Common Express.js Mistakes Beginners Make — Don't Do This vs Do This
Common Express.js Mistakes Beginners Make — Don't Do This vs Do This
js remove nested property from object
js remove nested property from object
Remove last element from an array in JavaScript
Remove last element from an array in JavaScript

This will delete the 'bar' property and its value from the 'foo' object.

What about Deeply Nested Properties?

Now, let's consider a more deeply nested object:

AD

How to return object literals from arrow functions in JavaScript
How to return object literals from arrow functions in JavaScript
Using switch Statements in JavaScript
Using switch Statements in JavaScript
Basic JavaScript Function Nesting
Basic JavaScript Function Nesting
JavaScript for Everything 🚀 | Complete JS Guide
JavaScript for Everything 🚀 | Complete JS Guide
Css object-fit property
Css object-fit property
🚀 Day 3/365 – JavaScript Object Methods Still confused about JavaScript Objects? 🤔 If you're serious about becoming a JavaScript developer, mastering Object Methods is a must. These methods help you manage, inspect, clone, and optimize objects in real-world applications. 📌 In today's cheat sheet you'll learn: ✅ Object.keys() ✅ Object.values() ✅ Object.entries() ✅ Object.assign() ✅ Object.freeze() ✅ Object.seal() ✅ hasOwnProperty() ✅ Object.prototype.hasOwnProperty.call() ✅ Object.defineP... Cheat Sheets, How To Become
🚀 Day 3/365 – JavaScript Object Methods Still confused about JavaScript Objects? 🤔 If you're serious about becoming a JavaScript developer, mastering Object Methods is a must. These methods help you manage, inspect, clone, and optimize objects in real-world applications. 📌 In today's cheat sheet you'll learn: ✅ Object.keys() ✅ Object.values() ✅ Object.entries() ✅ Object.assign() ✅ Object.freeze() ✅ Object.seal() ✅ hasOwnProperty() ✅ Object.prototype.hasOwnProperty.call() ✅ Object.defineP... Cheat Sheets, How To Become
an image of a computer screen with the text using pop to remove items on it
an image of a computer screen with the text using pop to remove items on it
JavaScript Object Methods Cheat Sheet
JavaScript Object Methods Cheat Sheet
Next.js Middleware Explained — Secure Routes, Handle Redirects & Protect Your App (2025 Guide)
Next.js Middleware Explained — Secure Routes, Handle Redirects & Protect Your App (2025 Guide)
a poster with some type of programming on it
a poster with some type of programming on it
CSS Transform property
CSS Transform property
Next.js Middleware Not Working? Fix These 7 Common Mistakes
Next.js Middleware Not Working? Fix These 7 Common Mistakes
let obj = {
  foo: {
    bar: {
      baz: 'qux'
    }
  }
};

To remove the 'baz' property, you can't simply use 'obj.foo.bar.baz'. JavaScript will throw an error because you're trying to read the value of 'bar' before it's defined. To navigate through such an object, you can use a combination of dot notation and optional chaining (?.).

AD

delete obj.foo?.bar.baz;

This will safely attempt to delete the 'baz' property from the 'bar' object nested inside the 'foo' object if both 'foo' and 'bar' exist.

Using a Function to Delete Nested Properties

Although dot notation is powerful, it can become cumbersome when dealing with multiple levels of nesting. In these cases, it's helpful to create a function that takes care of the deletion process. Here's a simple recursive function that deletes any specified property, regardless of how deeply it's nested:

Function for Deleting Nested Properties

Consider the following function:

function deleteProperty(obj, prop) {
  for (let key in obj) {
    if (typeof obj[key] === 'object' && obj[key] !== null) {
      deleteProperty(obj[key], prop);
    } else if (key === prop) {
      delete obj[key];
    }
  }
  return obj;
}

To use this function, simply pass the object and the property you want to delete as arguments:

deleteProperty(obj, 'foo.bar.baz');

This will delete the 'baz' property, regardless of how deeply it's nested.

Some Limitations

While this function is powerful, it has a few limitations. First, it doesn't handle arrays or objects with dynamic properties. If you're dealing with an array, you may want to use Array.prototype.filter() or similar methods to create a new array without the unwanted property.

Second, this function doesn't handle objects with dynamically generated property names. If you're dealing with such an object, you may need to use a combination of techniques, such as arrays or a mapping function, to handle the deletion.

In many cases, removing nested properties is a matter of navigating to the property and deleting it. With the right tools and a bit of creativity, you can handle even the most complex deletion tasks.

AD