-2

How can I get the id of an object?

In this example I want to get the 'ok' and store it in a variable. I know how to access the data.I need to access the id and store it for some purpose.

I use Angular so the console log is like this 'console.log (this.Data');'

{ok: Array(2)}
ok: Array(2)
.
.
.

Retry
  • 43
  • 1
  • 3
  • 11

3 Answers3

0

Always depends in your JSON structure but if you want the object inside the 'ok' key you can access through the key, for example:

{
  "ok": Array(2) 
}

You can access with the key associated at the object:

this.Data.ok;
this.Data[ok];
  • I mean i need to get only the id (i know how to get the data) i just want to get the array id which is 'ok' in this case. – Retry May 13 '19 at 15:38
  • Check this link: https://stackoverflow.com/questions/8430336/get-keys-of-json-object-in-javascript – user3841785 May 13 '19 at 16:54
0

Ok i find the solution.I changed the object.I set an 'id' and a 'data' field.Now i can access it like this.Data.id and this.Data.data.

Retry
  • 43
  • 1
  • 3
  • 11
0

You can get every key from your JSON with Object.keys. Object.keys(this.data)

Hackensack
  • 27
  • 3