How to set all values of an object to null in JavaScript?

前端 未结 7 468
北荒
北荒 2021-01-01 15:03

I need to set all properties of some object to null. But the object can be very big, so I can\'t just do it one by one.

How to set all properties at onc

7条回答
  •  生来不讨喜
    2021-01-01 15:58

    But the object can be very big, so I can't just do it one by one.

    By "big" do you mean "millions of properties" and you are concerned about performance? Or do you mean "a bunch of properties you don't know the names of, and/or don't want to have list out"?

    How to set all properties at once?

    You can't. One way or another, you have to loop.

    Instead of mutating an existing object, consider creating a new, empty object. Its property values will be undefined, but that could work depending on your code structure.

提交回复
热议问题