user3058870
user3058870

Reputation: 83

Javascript access array within object

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.

Upvotes: 0

Views: 68

Answers (3)

Dmitry Yudin
Dmitry Yudin

Reputation: 978

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

It seems that there is no key "d" in "tzOffset",because it is null.

Upvotes: -1

sandip rana
sandip rana

Reputation: 66

You can access the d from event['start']['d']['d']), Because tzOffset doesn't have any value to access.

Upvotes: 0

Bemwa Malak
Bemwa Malak

Reputation: 1349

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']);

Upvotes: 2

Related Questions