0

So I have an array of objects called 'test', that when I print on console.log, it shows like this on chrome inspect:

[]

0: Object name

variable 1: "value1"
variable 2: "value2"

1: Object name

variable 1: "value1"
variable 2: "value2"

If I try to console.log(test[0]), it's supposed to show the first data, variable 1 and 2, but it returns undefined.

But if I try to run the same command on chrome inspect (test[0]), it return the correct values...

I've tried to iterate through the array, but it doesn't work, if I try to print "test.length", on the code it returns 0, but if I run the same command on chrome inspect it returns 2...

My guess is that it has something to do with the first line that shows on chrome inspect (the "[]" before it opens to the 0, 1...).

Any ideas??

Lucas Heise
  • 139
  • 3
  • 12
  • 1
    We can't help you without **actual code**. – Dai Sep 05 '21 at 00:42
  • I'll recreate the objects to simulate the error, couldn't post the original one here because it contained some sensitive data – Lucas Heise Sep 05 '21 at 00:49
  • 1
    it's probably because the console evaluates the content of an array when you inspect it - if when you logged it it was empty, (length 0) then add to it asynchronously, the contents will be shown in the console, but the contents still won't be available where you logged it, because asynchrony – Bravo Sep 05 '21 at 01:22
  • 1
    "*it shows like this on chrome inspect: `[]`*" - that's an empty array. – Bergi Sep 05 '21 at 01:35

1 Answers1

-1

try putting your code to

$(document).ready(function () {
console.log(test[0])
});
kaung htet naing
  • 101
  • 1
  • 1
  • 7