mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-06-28 21:00:56 +08:00
feat(curriculum): Add interactive examples to How Can You Remove Properties from an Object (#63302)
Co-authored-by: Ilenia <26656284+ilenia-magoni@users.noreply.github.com>
This commit is contained in:
parent
bcd2740fc8
commit
c228f6d1dc
@ -5,12 +5,14 @@ challengeType: 19
|
||||
dashedName: how-can-you-remove-properties-from-an-object
|
||||
---
|
||||
|
||||
# --description--
|
||||
# --interactive--
|
||||
|
||||
There are several ways to remove properties from an object, with the `delete` operator being the most straightforward and commonly used method.
|
||||
|
||||
When you use `delete`, it removes the selected property from the object. Here's an example of how to use the `delete` operator:
|
||||
|
||||
:::interactive_editor
|
||||
|
||||
```js
|
||||
const person = {
|
||||
name: "Alice",
|
||||
@ -23,10 +25,14 @@ delete person.job;
|
||||
console.log(person.job); // undefined
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
In this example, we start with a `person` object that has three properties: `name`, `age`, and `job`. Then, we use the `delete` operator to remove the `job` property. After the deletion, the `person` object no longer has the `job` property.
|
||||
|
||||
Another way to remove properties is by using destructuring assignment with rest parameters. This approach doesn't actually delete the property, but it creates a new object without the specified properties:
|
||||
|
||||
:::interactive_editor
|
||||
|
||||
```js
|
||||
const person = {
|
||||
name: "Bob",
|
||||
@ -41,6 +47,8 @@ const { job, city, ...remainingProperties } = person;
|
||||
console.log(remainingProperties);
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
In this example, we use destructuring to extract `job` and `city` from the `person` object, and collect the remaining properties into a new object called `remainingProperties`. This creates a new object without the `job` and `city` properties.
|
||||
|
||||
Understanding how to remove properties from objects is an important skill in JavaScript programming. It allows you to manipulate objects dynamically, and clean up unnecessary data.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user