I create a array in Javascript. I want to use the array for key-value pair.
I can successfully add new items and delete items but the length of it is always 0.
Actually the problem I faced is when I want to convert it to a JSON string, it shows empty string: "[]". I just wonder if they are related. And I would also want to know why I cannot convert it.
Below is the debug information, It show the array contain 3 objects but the length is 0. The browser is Firefox 44.0 for Ubuntu.
Thanks!
Fellow @georg idea, I limited the index into 1000 and leave the rest code as before. Everything works including the JSON part. The next picture shows that in my array, there are actual 4 elements, the maximum index is 805.
The newest finding, the array is really as long as the largest index number. The rest of the element is Null.
This is one of many oddities in javascript.
> a = []
[]
> a[4294967295] = 1
1
> a.length
0 // wtf?
The problem is that Array.length
is limited to 32 bits, so its max value is 2^32-1 = 4294967295
. If you provide an index greater than or equal to 4294967295
, the engine creates a new entry in the array, as usual, but refuses to update its length
- without throwing any error. So you end up with a bag of properties, and the length
equal to 0.
As others suggested, you will be better off using objects if you have such large numeric indexes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With