简体   繁体   English

javascript数组索引大整数

[英]javascript array indexing with large integers

I have a strange problem. 我有一个奇怪的问题。 I need a multidimensional javascript array that has numeric indexes eg: 我需要一个具有数字索引的多维javascript数组,例如:

  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. 唯一是一个8位整数。 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. 因此,如果我不将它包装在引号内,则ListUI_col_orders数组将变得非常大,因为如果unique = 85481726则javascript将在85481726th索引处的新空数组之前用85481725未定义元素填充数组。

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. ...因为如果unique = 85481726那么javascript将在85481726th索引的新空数组之前用85481725未定义元素填充数组。

No, it won't. 不,它不会。 JavaScript standard arrays are sparse. JavaScript标准数组很稀疏。 Setting an element at index 85481725 results in an array with one entry, and a length value of 85481726 . 在索引85481725处设置元素85481725导致数组具有一个条目,并且length值为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 . unique85481726 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 . 由于它不符合数组索引的定义,因此不会影响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. 你在那里试过的钥匙是85481726"85481726" (带双引号)和85481726 Note that you never tried '85481726' (with single quotes), which is the key you used originally. 请注意,您从未尝试使用'85481726' (带单引号),这是您最初使用的密钥。 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. 不要担心数组length是一个很大的数字,JavaScript数组本质上是稀疏的。 Adding an entry with a large index does not create several thousand undefined entries in front of it. 添加具有大索引的条目不会在其前面创建数千个undefined条目。

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. 虽然length属性是10 ,但是数组中只有一个条目,在索引9处。这不仅仅是诡辩,而是实际的技术真相。 You can tell using in or hasOwnProperty : 你可以告诉使用inhasOwnProperty

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 . 当您尝试检索不存在的数组元素时,该检索的结果是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 . 只是尝试检索不存在的元素会返回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() . 最后注意:对于使用[]new Array()创建的标准数组,所有这些都是正确的。 In the next spec, JavaScript is gaining true arrays, created with constructors like Int32Array and such. 在下一个规范中,JavaScript正在获得真正的数组,使用像Int32Array等构造函数创建。 Those are real arrays. 那些是真正的数组。 (Many engines already have them.) (许多引擎已经拥有它们。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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