0

I want to access a variable value of a Javascript object. The below image is a result of a console.log: console.log(variable).

I tried: variable[0] -> returns undefined Also tried converting using JSON.stringify -> shows '[]' on the console

Here's the screenshot of the value I want to get:

enter image description here

Eddie
  • 26,593
  • 6
  • 36
  • 58
Jeram
  • 15
  • 6
  • If `variable[0]` returns undefined then I have no idea what is going on, but you should be able to access the value using `variable[0].name`, because this is an object inside of an array. – rafaelgomesxyz Mar 12 '18 at 02:59
  • 1
    perhaps there's an asynchronous thing happening, and when you try to access that item, it isn't there yet (see the blue `i` next to `[]`) - the fact that `JSON.stringify -> shows '[]' on the console` suggests you are trying to access asynchronous result of some AJAX call synchronously - but without **any code at all** it's a guess – Jaromanda X Mar 12 '18 at 03:00
  • If it is an asynchronous thing, per Jaromanda, you _might_ be able to right click it and see if it gives you an option to save as a global variable in the console. Then you can usually access it with something like `temp1`. Not sure if that option exists in the console, though-- I've only ever used it from the debugger. – Alexander Nied Mar 12 '18 at 03:04
  • 1
    If you can console.log `variable`, you can access `variable[0]`. So I'd wager it's an issue with your code. Try sharing a minimal version of your code. – Evert Mar 12 '18 at 03:06
  • @JaromandaX is right, the fact that it's logged as `[]` means that it's empty at the moment it gets `console.log`ed and only gets filled in at a later point. When you expand it in the console you see it as it is **right now**, not as it was when it got logged. – Lennholm Mar 12 '18 at 03:21
  • 1
    Um? Where is your code? This should have been the first comment. – Darkrum Mar 12 '18 at 03:45
  • Usually such badly asked questions get down–voted out of existence, I prefer to just vote to close. Where is the code? – RobG Mar 12 '18 at 04:25
  • Yeah. Jaromanda X is right. You can find the same issue here: https://stackoverflow.com/questions/23429203/weird-behavior-with-objects-console-log Thank you for the comments guys. I appreciate it – Jeram Mar 12 '18 at 06:53

1 Answers1

0

Assuming the array variable is the array of two objects, to get the string "tag1", you should try variable[0].name.

Vappor Washmade
  • 423
  • 4
  • 14