Challenge: Combine Objects with the Spread Operator
Swipe to show menu
Task
Create a script that performs the following tasks:
- Merge the properties of two objects,
personInfoandjobInfo, and store them in a new object namedfullInfo. - Add a new property to the
fullInfoobject namedisRetiredwith a value offalse. - Use a
for...inloop to iterate throughfullInfo, and log each property and its corresponding value in the format:[property]: [value].
12345678910111213141516171819202122const personInfo = { name: "Ferry", age: 62, city: "Caracas", }; const jobInfo = { experience: 7, occupation: "Speech-Language Pathologist", }; // Task 1: merge two objects const fullInfo = { ...___, ___, ___: ___, // Task 2: add the property }; // Task 3: log each property and its value for (let key in ___) { console.log(`${___}:`, ___[key]); }
Expected output:
name: Ferry
age: 62
city: Caracas
experience: 7
occupation: Speech-Language Pathologist
isRetired: false
- Use the spread operator (
{ ... }) to merge properties frompersonInfoandjobInfointofullInfo. - After merging, add a new property to
fullInfo. - Iterate through
fullInfousing afor...inloop to log the properties and their values.
1234567891011121314151617181920const personInfo = { name: "Ferry", age: 62, city: "Caracas", }; const jobInfo = { experience: 7, occupation: "Speech-Language Pathologist", }; const fullInfo = { ...personInfo, ...jobInfo, isRetired: false, }; for (let key in fullInfo) { console.log(`${key}:`, fullInfo[key]); }
Everything was clear?
Thanks for your feedback!
Section 1. Chapter 18
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Section 1. Chapter 18