Computer >> Computer tutorials >  >> Programming >> Javascript

Remove property for all objects in array in JavaScript?


To remove property, use delete keyword in JavaScript.

Example

Following is the code −

var values = [
   {
      "firstName": "John",
      "lastName":"Smith"
   },
   {
      "firstName": "David",
      "lastName":"Miller"
   },
   {
      "firstName": "Adam",
      "lastName":"Smith"
   }
];
values.forEach(function(obj){ delete obj.lastName });
console.log(values);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo312.js.

Output

This will produce the following output −

PS C:\Users\Amit\javascript-code> node demo312.js
[
   { firstName: 'John' },
   { firstName: 'David' },
   { firstName: 'Adam' }
]