lukeed / dset

A tiny (194B) utility for safely writing deep Object values~!
MIT License
754 stars 22 forks source link

Possibility to delete properties by path? #24

Closed manuschillerdev closed 3 years ago

manuschillerdev commented 3 years ago

Is it possible to not only set values, but also delete properties at a given path?

const obj = {
  items: [
    { 
      firstName: "peter",
      lastName: "parker"
    }
  ]
}

What I would need in my case would be:

delete obj.items[0].lastName;

Obviously I can set the value to null or undefined using set:

dset(obj, "items[0].lastName", null)

but I actually need to be able to delete the property completely. Is this possible using dset?

lukeed commented 3 years ago

Hello,

No, setting to undefined directly would be best bet. Other option is to get the value of the parent object, delete whatever property, and then set it back:

let item = dlv(obj, "items.0");
delete item.lastname;
dset(obj, "items.0", item);

Hope that helps