Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to delete properties by path? #24

Closed
manuschillerdev opened this issue Jul 7, 2021 · 1 comment
Closed

Possibility to delete properties by path? #24

manuschillerdev opened this issue Jul 7, 2021 · 1 comment

Comments

@manuschillerdev
Copy link

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
Copy link
Owner

lukeed commented Jul 7, 2021

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

@lukeed lukeed closed this as completed Jul 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants