1

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.

11
  • Please post sample data as text. Commented Aug 3, 2021 at 19:16
  • @Unmitigated sorry my bad will do that now in blockquote format. Commented Aug 3, 2021 at 19:19
  • @Unmitigated have made the edit you can see it. Commented Aug 3, 2021 at 19:28
  • console.log(courseWork[0].sub['101319245169270376329'].late) it should be true I think Commented Aug 3, 2021 at 19:42
  • 2
    I am not understanding your question. Your question says you are trying to access a property called true? console.log(courseWork[0].sub.true); I don't see a property called true in the sample object you have pasted.
    – abhinav
    Commented Aug 3, 2021 at 19:46

2 Answers 2

2

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; }

1
  • Will get back to you as well after trying it and if it seems a good answer too. Thanks for spending your time on it. Commented Aug 3, 2021 at 20:08
1

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

4
  • Thanks for your time Yuri. Will give it a try and get back to you if it works out. Commented Aug 3, 2021 at 19:56
  • Just with a slide modification the answer you suggest works Yuri. Thank you for your time. I needed to change my code to this for it to work. console.log(courseWork[0].sub['101319245169270376329'][0].late); Though I accept your answer. Commented Aug 3, 2021 at 20:03
  • This is exactly about how the parts of the samples of your data were joined: sub:{'123':{}} or sub:'{123':[{}]}. It was not quite clear from the question. Commented Aug 3, 2021 at 20:08
  • My bad if I was not able to explain it in proper way. thanks for your time though Yuri. Commented Aug 3, 2021 at 20:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.