Home >Web Front-end >JS Tutorial >How Can I Remove Properties from a JavaScript Object?
Removing Properties from JavaScript Objects
Removing properties from JavaScript objects is a common task, and there are several ways to approach this.
One option is to use the delete keyword. The delete keyword mutates the original object and removes the specified property. For example, in the provided object myObject, to remove the regex property:
delete myObject.regex;
Alternatively, you can use the bracket notation to access the property and assign it to undefined:
myObject['regex'] = undefined;
Another method is to use the Object.assign() function. This function creates a new object with the specified properties. You can use it to create a new object without the unwanted property:
let newObject = Object.assign({}, myObject); delete newObject.regex;
Whichever method you choose, remember that the delete keyword mutates the original object, while the other methods create a new object.
The above is the detailed content of How Can I Remove Properties from a JavaScript Object?. For more information, please follow other related articles on the PHP Chinese website!