Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Chrome Console - What does "Array(0)" mean?

I am using Console.log to identify the array values of a function. While examining the console I see a number of places where Array(0) exists:

enter image description here

In particular, I have created an array of key value pairs (see "x" and "testedElements" (same object) at the bottom of the picture above.)

When I expand "x" out that "Array(0)" sits at the top of an array element... I am unsure what "Array(0) means.. does it signify that this element is an array? enter image description here

I was actually trying to recreate the structure of "full MENU" at the top of the console picture but I have "Array(0)" displaying in the middle of testedElements/x...

like image 612
si2030 Avatar asked Oct 28 '22 21:10

si2030


1 Answers

That's how Chrome displays an 0-length array in value summaries in the console. Empty arrays can still contain fields due to the nature of JavaScript.

var obj = {};
obj.array = [];
obj.array.myField = 1;
console.log(obj);

This will log the following in the console:

> {array: Array(0)}

And when I expand it:

{array: Array(0)}
  array: Array(0)
    myField: 1
    length: 0
    __proto__: Array(0)
  __proto__: Object

This shows that named fields are not items in an array.

If you want an associative array (Array with named indexes), you should use plain JavaScript objects.

var obj = {};
obj.A = 10;
like image 104
Wazner Avatar answered Nov 01 '22 13:11

Wazner