⚠️ This lesson is retired and might contain outdated information.

Remove unwanted properties from a JavaScript with ES6 spread operator

Tomasz Łakomy
InstructorTomasz Łakomy
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 2 years ago

In this lesson we are going to learn how to remove unwanted properties from a JavaScript object following an example of an object containing sensitive data (such as password and bankBalance) and removing this kind of data from it.

To achieve that we are going to use two ES6 features: destructuring an object and the spread operator.

Instructor: [00:00] In this example we have an object containing some sensitive data about a user. We have a name, a surname, a password, which is stored in plain text, the user ID, and the bank balance.

[00:10] What we'd like to do is create a user data object which is going to contain all the user data but with the exception of password and bank balance, because this is not something that we would like to make public.

[00:21] To do that, destructure the sensitive data object as follows. We're going to do const password, bank balance, and we're going to spread the rest of this sensitive data to a new one that we're going to call user data. It's going to be equal to sensitive data.

[00:38] This way we get a brand new JavaScript object which contains everything that the sensitive data had but without the password and bank balance because those properties are destructured separately.

tsvika
tsvika
~ 4 years ago

wouldn't this cause memory leaks for unused vars?

Markdown supported.
Become a member to join the discussionEnroll Today