Challenge: Retrieve Object Property Values
Task
You're given an object, user
, representing a person's hobbies and interests. Your task is to access and retrieve specific details from the object.
Access and log the person's name.
Access the
indoor
hobby and log it to the console.Store the person's age in a variable.
Access the favorite music genre and log it.
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const user = {
name: "Sarah",
age: 32,
hobbies: {
outdoor: "hiking",
indoor: "painting",
},
preferences: {
music: "jazz",
movieGenre: "sci-fi",
},
};
// Task 1
console.log(___); // person's name
// Task 2
console.log(___); // indoor hobby
// Task 3
const userAge = ___;
console.log(userAge); // person's age
// Task 4
console.log(___); // music genre
12345678910111213141516171819202122232425const user = { name: "Sarah", age: 32, hobbies: { outdoor: "hiking", indoor: "painting", }, preferences: { music: "jazz", movieGenre: "sci-fi", }, }; // Task 1 console.log(___); // person's name // Task 2 console.log(___); // indoor hobby // Task 3 const userAge = ___; console.log(userAge); // person's age // Task 4 console.log(___); // music genre
Expected output:
python91234Sarahpainting32jazz
Use dot notation to access properties within an object.
To access properties within nested objects and arrays, chain the dot notation and square brackets (if needed).
Use assignment with dot notation to store property values in variables.
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const user = {
name: "Sarah",
age: 32,
hobbies: {
outdoor: "hiking",
indoor: "painting",
},
preferences: {
music: "jazz",
movieGenre: "sci-fi",
},
};
// Task 1
console.log(user.name); // person's name
// Task 2
console.log(user.hobbies.indoor); // indoor hobby
// Task 3
const userAge = user.age;
console.log(userAge); // person's age
// Task 4
console.log(user.preferences.music); // music genre
123456789101112131415161718192021222324const user = { name: "Sarah", age: 32, hobbies: { outdoor: "hiking", indoor: "painting", }, preferences: { music: "jazz", movieGenre: "sci-fi", }, }; // Task 1 console.log(user.name); // person's name // Task 2 console.log(user.hobbies.indoor); // indoor hobby // Task 3 const userAge = user.age; console.log(userAge); // person's age // Task 4 console.log(user.preferences.music); // music genre
Tudo estava claro?
Obrigado pelo seu feedback!
Seção 2. Capítulo 6
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo