Getting a subset of properties from an object with destructuring and the spread operator

In response to yesterday’s article on object destructuring, someone asked how to get the rest of the properties that you didn’t destructure into variables as their own object.
For example, if you have an object like this…
let movies = { disney: ‘Moana’, pixar: ‘Up’, dreamworks: ‘How to Train Your Dragon’, nickelodeon: ‘Wonder Park’ }; How do you pull disney and pixar into their own variables, and create an object with dreamworks and nickelodeon?


This content originally appeared on Go Make Things and was authored by Go Make Things

In response to yesterday’s article on object destructuring, someone asked how to get the rest of the properties that you didn’t destructure into variables as their own object.

For example, if you have an object like this…

let movies = {
	disney: 'Moana',
	pixar: 'Up',
	dreamworks: 'How to Train Your Dragon',
	nickelodeon: 'Wonder Park'
};

How do you pull disney and pixar into their own variables, and create an object with dreamworks and nickelodeon?

For that, we can combine object destructuring with the spread operator. Prefix one last variable with three dots (...), and it will be assigned an object with the remaining properties of your object that weren’t destructured.

let {disney, pixar, ...everyoneElse} = movies;

Here’s a demo.

🔥 Join the Vanilla JS Academy! A new session starts on January 30. Click here to learn more.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2023-01-20T14:30:00+00:00) Getting a subset of properties from an object with destructuring and the spread operator. Retrieved from https://www.scien.cx/2023/01/20/getting-a-subset-of-properties-from-an-object-with-destructuring-and-the-spread-operator/

MLA
" » Getting a subset of properties from an object with destructuring and the spread operator." Go Make Things | Sciencx - Friday January 20, 2023, https://www.scien.cx/2023/01/20/getting-a-subset-of-properties-from-an-object-with-destructuring-and-the-spread-operator/
HARVARD
Go Make Things | Sciencx Friday January 20, 2023 » Getting a subset of properties from an object with destructuring and the spread operator., viewed ,<https://www.scien.cx/2023/01/20/getting-a-subset-of-properties-from-an-object-with-destructuring-and-the-spread-operator/>
VANCOUVER
Go Make Things | Sciencx - » Getting a subset of properties from an object with destructuring and the spread operator. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/01/20/getting-a-subset-of-properties-from-an-object-with-destructuring-and-the-spread-operator/
CHICAGO
" » Getting a subset of properties from an object with destructuring and the spread operator." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2023/01/20/getting-a-subset-of-properties-from-an-object-with-destructuring-and-the-spread-operator/
IEEE
" » Getting a subset of properties from an object with destructuring and the spread operator." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2023/01/20/getting-a-subset-of-properties-from-an-object-with-destructuring-and-the-spread-operator/. [Accessed: ]
rf:citation
» Getting a subset of properties from an object with destructuring and the spread operator | Go Make Things | Sciencx | https://www.scien.cx/2023/01/20/getting-a-subset-of-properties-from-an-object-with-destructuring-and-the-spread-operator/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.