简体   繁体   中英

javascript array indexing with large integers

I have a strange problem. I need a multidimensional javascript array that has numeric indexes eg:

  MyArray = new Array();

  $(".list-ui-wrapper").each(function(){
       var unique = $(this).attr('id').replace(/[^\d.]/g,'');
       MyArray ["'"+unique+"'"] = new Array();
  });

The unique is a 8 digit integer. So if I dont wrap it inside the quotes, the ListUI_col_orders array will become extremly big, because if the unique = 85481726 then javascript will fill the array with 85481725 undefined elements before the the new empty array at the 85481726th index.

My problem is that later if i generate the unique again i cannot access the array anymore:

var unique = $(this).attr('id').replace(/[^\d.]/g,'');
console.log(MyArray [unique]); // undefined
console.log(MyArray ['"'+unique+'"']); // undefined
console.log(MyArray [unique.toString()]); // undefined

Any tips?

If you are going to use an array that's mostly sparse, then use a Hash table instead.

eg, set your variable as follows:

ListUI_col_Orders = {};

Then your indexing will be a key, so you don't have to worry about all the interstitial elements taking up space.

...because if the unique = 85481726 then javascript will fill the array with 85481725 undefined elements before the the new empty array at the 85481726th index.

No, it won't. JavaScript standard arrays are sparse. Setting an element at index 85481725 results in an array with one entry, and a length value of 85481726 . That's all it does. More: A myth of arrays

The problem is that you're trying to retrieve the information with a different key than the key you used to store it. When storing, you're using a key with single quotes in it (actually in the key), on this line of code:

MyArray ["'"+unique+"'"] = new Array();

Say unique is 85481726 . That line then is equivalent to this:

MyArray ["'85481726'"] = new Array();

Note that the key you're using (the text between the double quotes) has ' at the beginning and end. The actual property name has those quotes in it. Since it doesn't fit the definition of an array index, it doesn't affect length . It's a non-index object property. (How can you add a property to an array that isn't an array index? Arrays are not really arrays, see the link above.)

Later, you never use that key when trying to retrieve the value:

var unique = $(this).attr('id').replace(/[^\d.]/g,'');
console.log(MyArray [unique]); // undefined
console.log(MyArray ['"'+unique+'"']); // undefined
console.log(MyArray [unique.toString()]); // undefined

The keys you tried there were 85481726 , "85481726" (with double quotes), and 85481726 again. Note that you never tried '85481726' (with single quotes), which is the key you used originally. Since you didn't use the same key, you didn't get the value.

Either use consistent quotes, or (much better) don't use quotes at all. Don't worry about the array length being a large number, JavaScript arrays are inherently sparse. Adding an entry with a large index does not create several thousand undefined entries in front of it.

All of that being said, unless you need the "array-ness" of arrays, you can just use an object instead. Arrays are useful if you use their features; if you're not using their array features, just use an object.


More about the sparseness of arrays: Consider this code:

var a = [];
a[9] = "testing";
console.log(a.length); // 10

Although the length property is 10 , the array has only one entry in it, at index 9. This is not just sophistry, it's the actual, technical truth. You can tell using in or hasOwnProperty :

console.log(a.hasOwnProperty(3)); // false
console.log("3" in a);            // false

When you try to retrieve an array element that doesn't exist, the result of that retrieval is undefined . But that doesn't mean there's an element there; there isn't. Just that trying to retrieve an element that doesn't exist returns undefined . You get the same thing if you try to retrieve any other property the object doesn't have:

var a = [];
a[9] = "testing";
console.log(a[0]);     // undefined
console.log(a[200]);   // undefined
console.log(a["foo"]); // undefined

Final note: All of this is true of the standard arrays created with [] or new Array() . In the next spec, JavaScript is gaining true arrays, created with constructors like Int32Array and such. Those are real arrays. (Many engines already have them.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM