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

前端 未结 7 499
北荒
北荒 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 16:04

    If object contains child object, if you want to set all child object properties to null, recursive solution is below.

    function setEmpty(input){
    
    let keys = Object.keys(input);
    
        for( key of keys ){
    
             if(typeof input[key] != "object" ){
                 input[key] = null;
             }else{
                 setEmpty(input[key]);
             }
        }
        return input;
    }
    
    0 讨论(0)
提交回复
热议问题