Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Combine Objects with the Spread Operator | Advanced Object Manipulation Techniques
JavaScript Data Structures

book
Challenge: Combine Objects with the Spread Operator

Task

Create a script that performs the following tasks:

  • Merge the properties of two objects, personInfo and jobInfo, and store them in a new object named fullInfo.
  • Add a new property to the fullInfo object named isRetired with a value of false.
  • Use a for...in loop to iterate through fullInfo, and log each property and its corresponding value in the format: [property]: [value].
const 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]);
}
12345678910111213141516171819202122
const 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]); }
copy

Expected output:

python
name: Ferry
age: 62
city: Caracas
experience: 7
occupation: Speech-Language Pathologist
isRetired: false
  1. Use the spread operator ({ ... }) to merge properties from personInfo and jobInfo into fullInfo.
  2. After merging, add a new property to fullInfo.
  3. Iterate through fullInfo using a for...in loop to log the properties and their values.
const 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]);
}
1234567891011121314151617181920
const 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]); }
copy

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 6
some-alt