- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove property for all objects in array in JavaScript?
The task we are going to perform in this articles is to “Remove property for all objects in array in JavaScript”.
A collection of key-value pairs constitutes an object in JavaScript. A key-value pair among them is referred to as an object property. Any data type, including Number, String, Array, Object, etc., can be used for both the keys and values of properties
There are two methods: one is mutable and uses the delete operator; the other is immutable and uses object restructuring. Let’s discuss this method.
The delete operator
The delete operator eliminates both the property's value and the actual property. The property must be added again in order to be utilised again after being deleted. Object properties can be used with the delete operator. Variables and functions are unaffected.
Example
In the following example we are using the delete method to delete an object in array.
<!DOCTYPE html> <html> <body> <p id = "tutorial"></p> <script> const arr = [ {id: 1, name: 'teja', test: 'abd'}, {id: 2, name: 'suri', test: 'msd'}, ]; arr.forEach(object => { delete object['name']; }); document.getElementById("tutorial").innerHTML= JSON.stringify(arr); </script> </body> </html>
When the script gets executed, it will generate an output consisting of an array displayed on the web-browser, with the object deleted from the actual array by using the delete method that gets triggered when the user executes the script.
Object destructuring
Using the object destructuring and rest syntax is another way to remove properties in an immutable manner without changing the original object.
Only one property may be deleted by the delete operator per call. Therefore, you need to make two delete calls, if you want to remove the age and gender properties. As an alternative, you can remove numerous properties with a single call to object destructuring.
Example
Considering the following example, we are using the object destructuring to remove object.
<!DOCTYPE html> <html> <body> <p id = "tutorial"></p> <script> const person = { firstName: "don", lastName: "bosco", gender: "Male", age: 21 }; const {age, gender, ...personTrimmed} = person; const json = JSON.stringify(personTrimmed); document.getElementById("tutorial").innerHTML=JSON.stringify(json); </script> </body> </html>
Example
Let’s look into the another example where we are using delete keyword to remove property, in JavaScript.
<!DOCTYPE html> <html> <body> <p id = "tutorial"></p> <script> var values = [ { "firstName": "John", "lastName":"Smith" }, { "firstName": "David", "lastName":"Miller" }, { "firstName": "Adam", "lastName":"Smith" } ]; values.forEach(function(obj){ delete obj.lastName }); document.getElementById("tutorial").innerHTML=JSON.stringify(values); </script> </body> </html>