Reputation: 387
How can I access properties within a nested array and object data structure.
I wish to access the data within the sub
property of the object. However as you can see the sub property values do feature objects as well. So here is my code where I am using loop to access the property within this objects which would look something like this as shown below:
[{
updateTime: '2021-08-01T10:31:12.997Z',
state: 'PUBLISHED',
description: '[Assignment Link]',
creationTime: '2021-08-01T10:00:32.502Z',
creatorUserId: '100720723991781953762',
maxPoints: 100,
assigneeMode: 'ALL_STUDENTS',
title: 'Chapter 10 - Challenge - Sounds',
topicId: '371133499749',
dueTime: {
hours: 12,
minutes: 29
},
courseId: '359355912330',
dueDate: {
year: 2021,
day: 2,
month: 8
},
submissionModificationMode: 'MODIFIABLE_UNTIL_TURNED_IN',
alternateLink: 'Links',
id: '375299410585',
workType: 'ASSIGNMENT',
sub: {
'101319245169270376329': [Object],
'113602874081893916075': [Object],
'109482297400381284245': [Object],
'116018616608318973354': [Object],
'113664444807142890993': [Object],
'114971581068847820301': [Object],
'102115961232295542434': [Object],
'101379903617328602973': [Object],
'110645572827894944226': [Object],
'116196654365604694016': [Object],
'111060187005391455662': [Object],
'109231821126887264833': [Object],
'111638802824371384480': [Object],
'107268429707932588376': [Object],
'113020667154770187233': [Object],
'102653891403954041925': [Object],
'105324491423107091552': [Object],
'101716831976886159513': [Object],
'100197750836727383685': [Object],
'109019166529420617094': [Object],
'115372484470281534681': [Object],
'114443976641819242498': [Object]
}
}]
I am trying to access this late and other properties of the object here. Here I am showing you this example by using the following code.
console.log(courseWork[0].sub);
However when I try to access its sub properties like late or state I am not getting them and I get a undefined in my console like this:
console.log(courseWork[0].sub.late);
{
alternateLink: 'Links',
courseWorkType: 'ASSIGNMENT',
courseId: '359355912330',
assignmentSubmission: {},
userId: '101319245169270376329',
courseWorkId: '375299410585',
id: 'Cg0I9eKUzyYQmZXajPYK',
submissionHistory: [Object],
state: 'CREATED',
late: true,
creationTime: '2021-08-01T10:31:45.071Z',
updateTime: '2021-08-01T10:31:45.036Z'
}
Now I am new at javascript and appscript and never dealt with such big data and objects. How can i access the data to this level.
Upvotes: 1
Views: 127
Reputation: 13432
Please consult documentations to Object.keys
, Object.values
and Object.entries
...
var obj = {
sub: {
'101319245169270376329': { late: true },
'113602874081893916075': { late: true },
'109482297400381284245': { late: false },
'116018616608318973354': { late: true },
'113664444807142890993': { late: true },
'114971581068847820301': { late: false },
'102115961232295542434': { late: true },
'101379903617328602973': { late: false },
}
};
Object
.entries(obj.sub)
.forEach(([key, value]) =>
console.log(`'${ key }' ... late: ${ value.late }`)
);
Object
.values(obj.sub)
.forEach(item => console.log(`late: ${ item.late }`));
.as-console-wrapper { min-height: 100%!important; top: 0; }
Upvotes: 2
Reputation: 14537
var obj = {
updateTime: '2021-08-01T10:31:12.997Z',
state: 'PUBLISHED',
description: '[Assignment Link]',
creationTime: '2021-08-01T10:00:32.502Z',
creatorUserId: '100720723991781953762',
maxPoints: 100,
assigneeMode: 'ALL_STUDENTS',
title: 'Chapter 10 - Challenge - Sounds',
topicId: '371133499749',
dueTime: {
hours: 12,
minutes: 29
},
courseId: '359355912330',
dueDate: {
year: 2021,
day: 2,
month: 8
},
submissionModificationMode: 'MODIFIABLE_UNTIL_TURNED_IN',
alternateLink: 'Links',
id: '375299410585',
workType: 'ASSIGNMENT',
sub: {
'101319245169270376329': {
alternateLink: 'Links',
courseWorkType: 'ASSIGNMENT',
courseId: '359355912330',
assignmentSubmission: {},
userId: '101319245169270376329',
courseWorkId: '375299410585',
id: 'Cg0I9eKUzyYQmZXajPYK',
submissionHistory: {},
state: 'CREATED',
late: true,
creationTime: '2021-08-01T10:31:45.071Z',
updateTime: '2021-08-01T10:31:45.036Z'
},
'113602874081893916075': {},
'109482297400381284245': {},
'116018616608318973354': {},
'113664444807142890993': {},
'114971581068847820301': {},
'102115961232295542434': {},
'101379903617328602973': {},
'110645572827894944226': {},
'116196654365604694016': {},
'111060187005391455662': {},
'109231821126887264833': {},
'111638802824371384480': {},
'107268429707932588376': {},
'113020667154770187233': {},
'102653891403954041925': {},
'105324491423107091552': {},
'101716831976886159513': {},
'100197750836727383685': {},
'109019166529420617094': {},
'115372484470281534681': {},
'114443976641819242498': {}
}
};
console.log(obj.sub['101319245169270376329'].late); // output: true
Upvotes: 1