EN
JavaScript - remove property from object
10
points
In JavaScript, it is possible to remove the property from an object in the following way.
Quick solution:
delete myObject.propertyName;
or:
delete myObject['propertyName']
or:
const property = 'propertyName';
delete myObject[property];
Practical examples
Example 1
// ONLINE-RUNNER:browser;
const studentObject = {
name: 'John',
age: 25,
country: 'USA',
};
// delete property
delete studentObject.country;
// display result
const studentJson = JSON.stringify(studentObject);
console.log(studentJson);
Output:
{"name":"John","age":25}
Example 2
// ONLINE-RUNNER:browser;
const studentObject = {
name: 'John',
age: 25,
country: 'USA',
};
// delete property
delete studentObject['country'];
// display result
const studentJson = JSON.stringify(studentObject);
console.log(studentJson);
Output:
{"name":"John","age":25}
Example 3
// ONLINE-RUNNER:browser;
const studentObject = {
name: 'John',
age: 25,
country: 'USA',
};
const property = 'country';
// delete property
delete studentObject[property];
// display result
const studentJson = JSON.stringify(studentObject);
console.log(studentJson);
Output:
{"name":"John","age":25}