Skip to content Skip to sidebar Skip to footer

Javascript - Removing Object Key Not Using Delete

This might be duplicated question, but I could not find any. I wanna remove a key of an object. Please check my fiddle first. Remove a key of an object delete obj.something; When

Solution 1:

You can try obj[key] = undefined;. It's roughly 100 times faster, according to: How do I remove a property from a JavaScript object?

Solution 2:

you can use spread operators to remove keys.

let obj = {a:1, b:2, c:3}
const {a, ...restObj} = obj; // this will make restObj with deleted a key.

This method is especially useful when dealing with immutable types. This way you would not mutate the original object and still get an object with deleted key

Post a Comment for "Javascript - Removing Object Key Not Using Delete"