← Back to challenges

ES6: Destructuring Objects VIII

JavaScriptHardlanguage_fundamentalsformattingobjects

Instructions

Using basic object destructuring you can assign variables name and email:

let { name, email } = { name: "John", email: "[email protected]" }

console.log(name)  // "John"
console.log(email)  // "[email protected]"

What if there were more properties but you didn't want to write variables for all of them and just wanted to stick them into another object and do something like this:

let { name, email, rest} = { name: "John", email: "[email protected]", city: "Phoenix", state: "AZ", country: "USA"}

rest ===  { city: "Phoenix", state: "AZ", country: "USA"} // true

There is something you have to do with the variable name rest in order to assign it an object containing the rest of the object properties.

Use the rest syntax to change the code so that rest = {city: "Phoenix", state: "AZ", address: "USA"}. Only edit the left side of the assignmet { name, email, rest }. Ignore the .toString() function (used for validation).

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: A Day at the Market