Modify an Object Nested Within an Object
Now let's take a look at a slightly more complex object. Object properties can be nested to an arbitrary depth, and their values can be any type of data supported by JavaScript, including arrays and even other objects. Consider the following:
let nestedObject = {
id: 28802695164,
date: 'December 31, 2016',
data: {
totalUsers: 99,
online: 80,
onlineStatus: {
active: 67,
away: 13,
busy: 8
}
}
};
nestedObject
has three properties: id
(value is a number), date
(value is a string), and data
(value is an object with its nested structure). While structures can quickly become complex, we can still use the same notations to access the information we need. To assign the value 10
to the busy
property of the nested onlineStatus
object, we use dot notation to reference the property:
nestedObject.data.onlineStatus.busy = 10;
Here we've defined an object userActivity
, which includes another object nested within it. Set the value of the online
key to 45
.
Tests
- Waiting: 1.
userActivity
should haveid
,date
anddata
properties. - Waiting: 2.
userActivity
should have adata
key set to an object with keystotalUsers
andonline
. - Waiting: 3. The
online
property nested in thedata
key ofuserActivity
should be set to45
- Waiting: 4. The
online
property should be set using dot or bracket notation.
/** * Your test output will go here */