Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to omit specific properties from an object in JavaScript

Is there a clean way to return a new object that omits certain properties that the original object contains without having to use something like lodash?

like image 951
Aweb Avatar asked Mar 25 '17 02:03

Aweb


People also ask

How do I remove a property from an object?

Remove Property from an ObjectThe delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.

How do you remove one property from an array of objects?

To remove a property from all objects in an array:Use the Array. forEach() method to iterate over the array. On each iteration, use the delete operator to delete the specific property. The property will get removed from all objects in the array.

Which method do we use to remove a property from an object in JavaScript?

The JavaScript delete operator removes a property from an object; if no more references to the same property are held, it is eventually released automatically.

How do you omit a key in an object?

Using the delete operator to remove a property The delete operator is used to remove the key from an object, and its key and value are removed from an object. Return true, if the key is removed, and return false if the key does not exist.


1 Answers

const { bar, baz, ...qux } = foo 

Now your object qux has all of the properties of foo except for bar and baz.

like image 95
Craig Gehring Avatar answered Sep 26 '22 08:09

Craig Gehring