Home > Blockchain >  Javascript access array within object
Javascript access array within object

Time:01-04

I have a javascript object called event which is output from the console.log below:

{
"title": "Test Title",
"location": "Test Location",
"isAllday": false,
"isPrivate": false,
"state": "Busy",
"start": {
    "tzOffset": null,
    "d": {
        "d": "2023-01-03T16:00:00.000Z"
    }
},
"end": {
    "tzOffset": null,
    "d": {
        "d": "2023-01-03T16:30:00.000Z"
    }
},
"id": "afdb82fd-fddd-58ce-bd0b-ab0beb2bce7b"

}

I can access some items through alert(event['title'] event['location']); I cannot access the nested items like start.tzOffset.d If I try alert(event['title'] event['start']['tzOffset']['d']); I get an error "Uncaught TypeError: Cannot read properties of null (reading 'd')"

Any help greatly appreciated.

CodePudding user response:

That's because tzOffset doesn't have a nested key called d inside of it. instead it is inside start object and inside d object there is a key called d so it needs to be like this:

alert(event['title']   event['start']['d']['d']);

CodePudding user response:

alert(event['title']   event['start']['d'])
  • Related