Skip to content

#298 Destructuring Objects2

Adam Kubiś edited this page Oct 28, 2026 · 1 revision

298 Destructuring Objects2

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 assignment { name, email, rest }. Ignore the.toString() function (used for validation).

Current source and tests | Catalog | Testing and tooling

Run this solution

From the cloned repository root, after installing dependencies:

node "Edabit/298 Destructuring Objects2.js"

Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.

Current implementation and tests

This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.

/*Using basic object destructuring you can assign variables name and email:
let { name, email } = { name: "John", email: "john@example.com" }
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: "john@example.com", 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 assignment { name, email, rest }. Ignore the.toString() function (used for validation).*/
const user = { name: "John", email: "[email protected]", city: "Phoenix", state: "AZ", country: "USA" }
const str = `({ name, email, ...rest } = user ).toString()`;
import { assertEquals } from '../testHelper.js';
const assignedUser = {};
({
	name: assignedUser.name,
	email: assignedUser.email,
	...assignedUser.rest
} = user);

function validUser() {
	return JSON.stringify(user) === JSON.stringify({ name: "John", email: "[email protected]", city: "Phoenix", state: "AZ", country: "USA" }) ? "valid" : "not valid"
}

function rightSideValidation() {
	return /\suser\s/.test(str) ? "valid" : "not valid"
}

assertEquals(validUser(), "valid", "The user object must not be changed")
assertEquals(rightSideValidation(), "valid", 'The right side of the assignment should remain as "user"')
assertEquals(assignedUser.name, "John")
assertEquals(assignedUser.email, "[email protected]")
assertEquals(assignedUser.rest, { city: "Phoenix", state: "AZ", country: "USA" }, "The variable rest has not been defined yet.")

Clone this wiki locally