Languages
[Edit]
EN

JavaScript - remove property from object

10 points
Created by:
ArcadeParade
666

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}

Alternative titles

  1. JavaScript - how to remove property from object?
  2. JavaScript - remove item from object
  3. JavaScript - remove key/value pair from object
  4. JavaScript - remove JSON object key and value
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join