Skip to content

Instantly share code, notes, and snippets.

@lalilaloe
Created April 25, 2024 19:54
Show Gist options
  • Save lalilaloe/90132e39a359172a4392c83549073029 to your computer and use it in GitHub Desktop.
Save lalilaloe/90132e39a359172a4392c83549073029 to your computer and use it in GitHub Desktop.
Javascript readonly nested array to writable

In case you ever need an readonly nested array to be writable again. It also checks if the object is configurable, TypeError: can't redefine non-configurable property. If it is not configurable there is no other way than to find another solution, such as recreating {...item}

Object.keys(new MyClass()).forEach(field => {
        if(Object.hasOwn(item, field)){
        // Check if the property is configurable
          const descriptor = Object.getOwnPropertyDescriptor(item, field);
          if (descriptor && !descriptor.configurable) {
              // Make the property configurable
              Object.defineProperty(item, field, { writable: true });
        }
      });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment