Challenge: Combine Objects with the Spread Operator
Task
Create a script that performs the following tasks:
Merge the properties of two objects,
personInfo
andjobInfo
, and store them in a new object namedfullInfo
.Add a new property to the
fullInfo
object namedisRetired
with a value offalse
.Use a
for...in
loop to iterate throughfullInfo
, and log each property and its corresponding value in the format:[property]: [value]
.
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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]);
}
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:
python9123456name: Ferryage: 62city: Caracasexperience: 7occupation: Speech-Language PathologistisRetired: false
Use the spread operator (
{ ... }
) to merge properties frompersonInfo
andjobInfo
intofullInfo
.After merging, add a new property to
fullInfo
.Iterate through
fullInfo
using afor...in
loop to log the properties and their values.
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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]);
}
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]); }
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 3. Capitolo 6
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione