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

前端 未结 7 543
北荒
北荒 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:44

    You can use Object.keys() as Nianyi Wang mentioned in his answer, or a for in, like this:

    for (key in obj) {
        if (obj.hasOwnProperty(key)) {
            obj[key] = null;
        }
    }
    

    But in this case you should check hasOwnProperty().

提交回复
热议问题