0

In variable called 'data' I have this data:

{"user":"peter","expires":1601630467}

I am trying to reach "expires" so I've done this:

console.log(`data expires value: ${data.['expires']}`);

But the above returns undefined even though when I do:

console.log(data); I get {"user":"peter","expires":1601630467}

What I'm I doing wrong here?

user13962846
  • 83
  • 2
  • 9
  • 3
    Either use dot notation `data.expires` or the bracket notation `data['expires']`. Don't mix them. – ruth Oct 01 '20 at 09:37

1 Answers1

3

You should either write

console.log(`data expires value: ${data['expires']}`);

or

console.log(`data expires value: ${data.expires}`);

as an object property can either be accessed this['way'] or this.way.

Romuald
  • 205
  • 1
  • 13
  • that's what I did in the first one...when I try data.expires I get error: Property 'expires' does not exist on type string – user13962846 Oct 01 '20 at 09:40
  • 1
    Then assuming that `data = {"user":"peter","expires":1601630467}` I can only guess that you've made some other mistake around. – Romuald Oct 01 '20 at 09:43
  • 1
    @user13962846 Then you are probably dealing with JSON. See the duplicate question on how to handle that. – str Oct 01 '20 at 09:49