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

前端 未结 7 545
北荒
北荒 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:54

    Another way of doing it, using Array.reduce. It does not overwriting the existing object. This only works if the object only have simple values.

    const newObj = Object.keys(originalObj).reduce(
      (accumulator, current) => {
        accumulator[current] = null; 
        return accumulator
      }, {});
    

提交回复
热议问题