Here is an example:
const initObject = { a: 0, b: 0, c: 0 } const { a, ...rest } = initObject
We're omitting property a
from the object, but then const a
is assigned a value, but never used - error from eslint (no-unused-vars). Is it possible to completely omit const a
?
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.
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.
You simply destructure the object into two parts: one part is the property you're trying to remove ( drugName in this case), and the other part is the rest of the object, that you want to keep ( drugWithoutName in this case).
Destructuring Objects What's even better, object destructuring can extract multiple properties in one statement. This makes the code clearer. The order of the properties does not matter.
A possible way is to use // eslint-disable-next-line no-unused-vars
e.g.
// eslint-disable-next-line no-unused-vars const { a, ...rest } = initObject
Or by using ignoreRestSiblings
The ignoreRestSiblings option is a boolean (default: false). Using a Rest Property it is possible to “omit” properties from an object, but by default the sibling properties are marked as “unused”. With this option enabled the rest property’s siblings are ignored.
e.g.
/*eslint no-unused-vars: ["error", { "ignoreRestSiblings": true }]*/ // 'a' is ignored because it has a rest property sibling. const { a, ...rest } = initObject;
More info about no-unused-vars
But if your goal is to remove the property a
, there is another way.
You can use delete
operator.
From MDN documentation
The JavaScript
delete
operator removes a property from an object
e.g.
const initObject = { a: 0, b: 0, c: 0 } const rest = { ...initObject }; // create a shallow copy delete rest.a; console.log(rest);
error from eslint (no-unused-vars).
The no-unused-vars rules has two configuration options that will help with your use case:
ignoreRestSiblings
option is a boolean that defaults to false
. When enabled, the rest property’s siblings are ignored. This is exactly what you need!The varsIgnorePattern
option specifies a regexp pattern for variable names not to be checked for usage. This allows us to make an exception for the common underscore identifier to explicitly mark unused variables with { "varsIgnorePattern": "^_" }
.
const { a:_, ...rest } = initObject; // ^^
Unfortunately you still need to avoid multiple declarations of the _
variable, so to omit multiple properties you'd need to do something like { a:_a, b:_b, ...rest } = …
.
Is it possible to completely omit
const a
?
A bad hack that completely avoids introducing any identifier would be to use
const { a:{}, ...rest } = initObject; // ^^^
that further destructures the .a
property value into an object, but for this you need to ensure that the property exists and doesn't hold a null
or undefined
value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With