1

Currently trying to delete an object within another object using the delete keyword as I've seen all around the web. The object I'm using is similar to the one below:

object = { NAME: {hello: 3838383, goodbye: 3474737}, NAME2: {hello: 3838383, goodbye: 3474737} }

Trying:

delete object.NAME

But I continually get: Cannot delete property 'NAME' of [object Object]. Any ideas?

3
  • I've just tried this in my console and worked, any other details that you can provide? Commented Dec 1, 2020 at 16:31
  • Well this response object is coming from an observable I subscribe to so perhaps it isn't a JSON object. Is there some type of objectfy function I can apply to it? Commented Dec 1, 2020 at 16:34
  • @paulgio - It's definitely not JSON, because it's not a string. :-) JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. Commented Dec 1, 2020 at 16:38

1 Answer 1

1

It sounds like NAME is a non-configurable property. That means you can't delete it I'm afraid, as deleting (removing) a property counts as "configuring" it (as does changing the configurable flag on the property).

You can see what the configuration of the property is by using Object.getOwnPropertyDescriptor. In your case it probably will either not show a configurable property at all (because false is the default) or more likely it'll show configurable: false.

Here's an example which shows using getOwnPropertyDescriptor and demonstrates the error you're getting:

"use strict";
const object = { };
Object.defineProperty(object, "NAME", {
    value: {hello: 3838383, goodbye: 3474737},
    configurable: false,
    writable: true
});
console.log(Object.getOwnPropertyDescriptor(object, "NAME"));
delete object.NAME;

If the property is writable, you can assign some other value to itm, such as null or undefined:

object.NAME = null;

But if it's also non-writable, you can't do that, either.

As a final resort, you could create a new object and only copy the properties to it that you want, and assign that new object to the variable or property you're using to refer to the object. For instance:

object = Object.fromEntries(
    Object.entries(object)
      .filter(([key]) => key !== "NAME")
);

Live Example:

"use strict";
let object = {
    NAME2: {hello: 3838383, goodbye: 3474737}
};
Object.defineProperty(object, "NAME", {
    value: {hello: 3838383, goodbye: 3474737},
    configurable: false, // Just here for emphasis, false is the default
    writable: false      // same
});
console.log(Object.getOwnPropertyDescriptor(object, "NAME"));

// Can't do this: delete object.NAME;
object = Object.fromEntries(
    Object.entries(object)
      .filter(([key]) => key !== "NAME")
);
console.log(object);
.as-console-wrapper {
    max-height: 100% !important;
}

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way around using Object.fromEntries?
@paulgio - You can loop over Object.entries or similar, but if the property is non-configurable, you can't delete it; if it's non-writable, you can't change it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.