DEV Community


Posted on

Easily delete Object Properties in JS with this Meta trick 🔥🔥


Reflect.deleteProperty(Object, key)

first it's metaprogramming and sounds way cooler than delete, on a serious note sometimes the delete keyword does not work as intended i don't know why, i was working on a cormmecial project recently and had may objects and nested objects, delete failed to delete the keys while deleting the value, and Reflect came to the rescue, it is built in and more efficient.

let ob = {name: "Sk", age: 22}
Reflect.deleteProperty(ob, "name")
//->  { age: 22 }

Enter fullscreen mode Exit fullscreen mode

Honestly Reflect.deleteProperty shines in nested objects

Discussion (0)