Delete multiple object properties?

后端 未结 9 946
天命终不由人
天命终不由人 2020-12-29 01:51

I create an object with multiple properties -

var objOpts = {
  option1: \'Option1\',
  option2: \'Option2\',
  option2: \'Option3\'
};

I t

9条回答
  •  离开以前
    2020-12-29 02:01

    One aditionnal otpion would be to use Object.assign to set the properties to either null or undefined depending on your use case. This does not delete the properties but clears them. So you would do either of these :

    // Modify the object directly
    Object.assign(objOpts, {option4: null, option5: null});
    

    OR

    // Create a copy of the object with the properties cleared 
    const newObject = Object.assign({}, objOpts, {option4: null, option5: null});
    

    You could also use an array to list the properties to clear with Array.reduce() to create the last param of Object.assign().

提交回复
热议问题