Skip to content

Commit

Permalink
Update index.md (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
VinayKrMittal authored Sep 3, 2023
1 parent 23ca627 commit aba5b74
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions posts/145-remove-property/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ const { property, ...restObject } = object;

B) The property name is dynamic:
```javascript
const name = 'property';
const { [name]: removedProperty, ...restObject } = object;
const propNameToRemove = 'property';
const { [propNameToRemove]: removedProperty, ...restObject } = object;
```

After applying the destructuring and rest syntax, `restObject` is going to contain the same properties as `object`, only without the removed property.
Expand All @@ -125,23 +125,23 @@ The statement `const { position, ...employeeRest } = employee` destructures the

Object destructuring with rest syntax is an immutable way of property removal: the original `employee` object isn't mutated. Rather a new object `employeeRest` is created which contains all the properties of `employee` but without the removed `position`.

If the property name to remove is determined dynamically, then you can use use the dynamic property name destructuring syntax:
If the property name to remove is determined dynamically, then you can use the dynamic property name destructuring syntax:

```javascript mark=7
const employee = {
name: 'John Smith',
position: 'Sales Manager'
};

const name = 'position';
const { [name]: removedProperty, ...employeeRest } = employee;
const propNameToRemove = 'position';
const { [propNameToRemove]: removedProperty, ...employeeRest } = employee;

console.log(employeeRest); // { name: 'John Smith' }
```

[Open the demo.](https://codesandbox.io/s/destructuring-rest-dynamic-m4jgf)

`const { [name]: removedProperty, ...employeeRest } = employee` let's you remove a property with dynamic name by collecting the properties, but removed one, into `employeeRest` object.
`const { [propNameToRemove]: removedProperty, ...employeeRest } = employee` let's you remove a property with dynamic property name by collecting the properties, but removed one, into `employeeRest` object.

What's interesting is that you can remove multiple properties at once using the destructuring and rest syntax:

Expand Down Expand Up @@ -169,4 +169,4 @@ The first mutable approach is to use the `delete object.property` operator.

The second approach, which is immutable since it doesn't modify the original object, is to invoke the object destructuring and spread syntax: `const {property, ...rest} = object`.

*Side challenge: what is the [time complexity](https://en.wikipedia.org/wiki/Time_complexity) of the property removal using `delete` and object rest syntax? Write your opinion in a comment below!*
*Side challenge: what is the [time complexity](https://en.wikipedia.org/wiki/Time_complexity) of the property removal using `delete` and object rest syntax? Write your opinion in a comment below!*

0 comments on commit aba5b74

Please sign in to comment.