4

Remove Properties from Object Javascript

 2 years ago
source link: https://thispointer.com/remove-properties-from-object-javascript/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

We often encounter a requirement to delete specific properties from a javascript object. This article demonstrates easy ways to delete single or multiple properties from a javascript object using different methods and example illustrations.

Table of Contents:

Remove Property from Object Javascript

Javascript’s delete operator will remove a property from a javascript object. If no references to the deleted property are found, it is released eventually.

Syntax:-

delete objectName.propertyName
delete objectName[propertyName]
delete objectName.propertyName
delete objectName[propertyName]

Example :-

Advertisements

vid5e62792b95ec8618094391.jpg?cbuster=1600267117
00:00/15:21
10 Sec
Lambda Functions in Python
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTYmOTUmMTM5OSZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow0znXNBpHA9MCZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMkMxQmMTMlMxQmMTM1NUYmMDMmN0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmpmMwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3RDqCNTxmMmMlMmU3RDqCNwYmMTqEN0I0QmMkMmImNTMlMmE3REZFRxUzZGyunWQ9JaVmZXJJpEFxZHI9MTQkLwE2NC42Ml4kNwQzqXNypyVBPU1irzyfoGEyMxY1LwAyMwAyMwuYMTEyM0IyMwBMnW51rCUlMHt4Ny82NCUlOSUlMEFjpGkyV2VvS2y0JTJGNTM3LwM2JTIjJTI4S0uUTUjyMxMyMwBfnWgyJTIjR2Vwn28yMwxyMwBDnHJioWUyMxY3Nl4jLwM4NwUhMTIjJTIjU2FzYXJcJTJGNTM3LwM2JzNmqXVcZD02MWI5NDM4Nwt5ZzQ4JzNioaRyoaRGnWkySWQ9MCZgZWRcYVBfYXyMnXN0SWQ9MCZgZWRcYUkcp3RJZD0jJzqxpHI9MCZaZHBlQ29hp2VhqD0znXNXZVBup3NHZHBlPTEzY2NjYT0jJzNwpGFDo25mZW50PSZwYaVmqGVlPTE2Mmx1MmE0MDA1MDAzqWyxPVNyn2yhZG9TUGkurWVlNwFvOTQmODpmMmVzNvZjqWJVpzj9nHR0pHMyM0EyMxYyMxZ0nGympG9coaRypv5wo20yMxZlZW1iqzUgpHJipGVlqGyypl1zpz9gLW9vnzVwqC1dYXZup2NlnXB0JTJGJzZfo2F0U3RuqHVmPWZuoHNyJzVcZHNjPXBlZWJcZA==

Remove personIdNumber property from the object { personFirstName: ‘George’, personLastName: ‘Smith’, dateOfBirth: ‘Nov 14 1984’ , city : ‘Santiago’, personIdNumber : 3434343}

Code:-

let personObject = { personFirstName : 'George',
personLastName : 'Smith',
dateOfBirth : 'Nov 14 1984' ,
city : 'Santiago',
personIdNumber : 3434343};
console.log("Before Delete: " );
console.log(personObject);
// using delete operator
delete personObject.personIdNumber;
console.log("After Delete: ");
console.log(personObject);
let personObject =  { personFirstName : 'George',
                      personLastName  : 'Smith',
                      dateOfBirth     : 'Nov 14 1984' ,
                      city            : 'Santiago',
                      personIdNumber  : 3434343};
console.log("Before Delete: " );
console.log(personObject);
// using delete operator
delete personObject.personIdNumber;
console.log("After Delete: ");
console.log(personObject);

Output:-

Before Delete:
personFirstName: 'George',
personLastName: 'Smith',
dateOfBirth: 'Nov 14 1984',
city: 'Santiago',
personIdNumber: 3434343
After Delete:
personFirstName: 'George',
personLastName: 'Smith',
dateOfBirth: 'Nov 14 1984',
city: 'Santiago'
Before Delete: 
{
  personFirstName: 'George',
  personLastName: 'Smith',
  dateOfBirth: 'Nov 14 1984',
  city: 'Santiago',
  personIdNumber: 3434343
}
After Delete: 
{
  personFirstName: 'George',
  personLastName: 'Smith',
  dateOfBirth: 'Nov 14 1984',
  city: 'Santiago'
}

Explanation:-

As the output shows after applying the delete operator to personObject on personIdNumber(personObject.personIdNumber), the property personIdNumber is not seen on the console.

Remove Property from Object Javascript Destructuring

Javascript’s destructuring assignment syntax allows the properties from objects or values from arrays to be unpacked to distinct variables.

Example :-

Create a new object without personIdNumber property from the object { personFirstName: ‘George’, personLastName: ‘Smith’, dateOfBirth: ‘Nov 14 1984’ , city : ‘Santiago’, personIdNumber : 3434343}

Code:-

let personObject = { personFirstName : 'George',
personLastName : 'Smith',
dateOfBirth : 'Nov 14 1984' ,
city : 'Santiago',
personIdNumber : 3434343};
//object destructuring
const {personIdNumber, ...newObject} = personObject;
console.log("Original Object: ")
console.log(personObject);
console.log("New Object: ")
console.log(newObject);
let personObject =  { personFirstName : 'George',
                      personLastName  : 'Smith',
                      dateOfBirth     : 'Nov 14 1984' ,
                      city            : 'Santiago',
                      personIdNumber  : 3434343};
//object destructuring
const {personIdNumber, ...newObject} = personObject;
console.log("Original Object: ")
console.log(personObject);
console.log("New Object: ")
console.log(newObject);

Output:-

Original Object:
personFirstName: 'George',
personLastName: 'Smith',
dateOfBirth: 'Nov 14 1984',
city: 'Santiago',
personIdNumber: 3434343
New Object:
personFirstName: 'George',
personLastName: 'Smith',
dateOfBirth: 'Nov 14 1984',
city: 'Santiago'
Original Object: 
{
  personFirstName: 'George',
  personLastName: 'Smith',
  dateOfBirth: 'Nov 14 1984',
  city: 'Santiago',
  personIdNumber: 3434343
}
New Object: 
{
  personFirstName: 'George',
  personLastName: 'Smith',
  dateOfBirth: 'Nov 14 1984',
  city: 'Santiago'
}

Explanation:-

Here, the new object(newObj) will contain all properties from the personObject except the property personIdNumber using destructing of personObject.

Note that destructuring can be used from ECMAScript6 onwards.

Remove Multiple Properties from Object Javascript

Example :-

Remove the properties personIdNumber, city from the object { personFirstName: ‘George’, personLastName: ‘Smith’, dateOfBirth: ‘Nov 14 1984’ , city : ‘Santiago’, personIdNumber : 3434343}

Code:-

//create a custom method to delete multiple properties
function deleteMultipleProperties (_object, _propertyArray)
for (const p of _propertyArray) {
(p in _object) && (delete _object[p]);
let personObject = { personFirstName : 'George',
personLastName : 'Smith',
dateOfBirth : 'Nov 14 1984' ,
city : 'Santiago',
personIdNumber : 3434343};
console.log("Before Delete:");
console.log(personObject);
// usage of the function deleteMultipleProperties
deleteMultipleProperties(personObject, ['city', 'personIdNumber']);
console.log("After Delete:");
console.log(personObject);
//create a custom method to delete multiple properties
function deleteMultipleProperties (_object, _propertyArray) 
{
  for (const p of _propertyArray) {
      (p in _object) && (delete _object[p]);
  }    
}
let personObject =  { personFirstName : 'George',
                      personLastName  : 'Smith',
                      dateOfBirth     : 'Nov 14 1984' ,
                      city            : 'Santiago',
                      personIdNumber  : 3434343};
console.log("Before Delete:");
console.log(personObject);
// usage of the function deleteMultipleProperties
deleteMultipleProperties(personObject, ['city', 'personIdNumber']);
console.log("After Delete:");
console.log(personObject);

Output:-

Before Delete:
personFirstName: 'George',
personLastName: 'Smith',
dateOfBirth: 'Nov 14 1984',
city: 'Santiago',
personIdNumber: 3434343
After Delete:
personFirstName: 'George',
personLastName: 'Smith',
dateOfBirth: 'Nov 14 1984'
Before Delete:
{
  personFirstName: 'George',
  personLastName: 'Smith',
  dateOfBirth: 'Nov 14 1984',
  city: 'Santiago',
  personIdNumber: 3434343
}
After Delete:
{
  personFirstName: 'George',
  personLastName: 'Smith',
  dateOfBirth: 'Nov 14 1984'
}

Explanation:-

  • Here, we use the same delete operator to delete the properties one-by-one. The only difference is that we have created a custom method deleteMultipleProperties().
  • _object: is the object from which the properties need to be deleted.
  • _propertyArray: is the array of properties to be deleted from the object.

I hope this article helped you delete a single property or multiple properties from a javascript object. Good Luck !!!

Advertisements


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK