简体   繁体   English

如何防止在Javascript中创建额外的数组元素?

[英]How can I prevent extra array elements being created in Javascript?

In PHP, if you do this 在PHP中,如果你这样做

$var = array(); 
$var[5000] = 1;
echo count($var);

It prints 1. 它打印1。

In JS it makes the "missing" elements. 在JS中它使“缺失”元素。

<script type="text/javascript">
    var fred = [];
    fred[10] = 1;
    alert(fred.length);
</script>

Alerts "11". 提醒“11”。

Can I prevent this? 我可以阻止这个吗? Is it good for anything? 对任何事都有好处吗? I'm a PHP coder trying to adapt to Javascript. 我是一个试图适应Javascript的PHP编码器。

EDIT: 编辑:

I'm working on an app that uses Google Maps v2 and markerManager. 我正在开发一款使用Google Maps v2和markerManager的应用。 The code has been working for ages, but a problem has suddenly arisen in Chrome (17.0.963.56 m) where markers seem to be duplicated (or more) and then rendering moved markers goes completely wrong, often followed by a browser freeze-up. 该代码已经工作了很长时间,但Chrome(17.0.963.56 m)突然出现了一个问题,其中标记似乎是重复的(或更多),然后渲染移动的标记完全错误,通常随后浏览器冻结。 Looking in the DOM using Firebug, I found gazillions of "undefined" elements in arrays under the grid_ variable in markerManager. 使用Firebug查看DOM,我在markerManager的grid_变量下的数组中找到了大量的“未定义”元素。 I figured that, if I could remove these, I might have slicker code, whether it fixes the marker problem or not. 我想,如果我可以删除它们,我可能会有更光滑的代码,无论它是否修复了标记问题。 Thanks. 谢谢。

As a PHP coder, you're accustomed to array keys being fairly arbitrary. 作为一个PHP编码器,你已经习惯了数组键是相当随意的。 In JavaScript however, if you assign an array element numerically that doesn't exist, the array will be allocated with empty elements up to the one you created. 但是,在JavaScript中,如果以数字方式分配不存在的数组元素,则将为数组分配空元素,直到您创建的元素。 That's the behavior you found. 这就是你找到的行为。 Objects, on the other hand, denoted by {} rather than [] , accept arbitrary property names (with caution) and function a little more closely to how you're accustomed PHP's array structures functioning. 另一方面,由{}而不是[]表示的对象接受任意属性名称(谨慎),并且更接近于您习惯PHP的数组结构的功能。 This is not to say that JavaScript objects (or object literals) are a direct analogue to PHP arrays.... 这并不是说JavaScript对象(或对象文字)是PHP数组的直接模拟....

If what you want is a data structure with a key called "10", that is a job for an object literal (though it isn't great practice to name object properties numerically. 如果你想要的是一个带有一个名为“10”的键的数据结构,那就是一个对象文字的作业(尽管用数字命名对象属性并不是很好的做法。

var fred = {};
fred["10"] = 1;

Note, the more normal syntax of dealing with an object literal is the object.property notation, but that is not valid for numeric properties: 注意,处理对象文字的更常规语法是object.property表示法,但这对数字属性无效:

// name as a property
fred.name = "fred";

// Syntax error...
fred.10 = 10;

// Use [] notation for numeric properties:
fred["10"] = 10;

Object literals do not have a length property, however. 但是,对象文字没有length属性。 So you cannot do: 所以你做不到:

fred.length;
// undefined

You may use an object instead of an array, but you loose some of the benefits of an array, like access to a length property. 您可以使用对象而不是数组,但是您可以放弃数组的一些好处,例如访问length属性。

<script type="text/javascript">
    var fred = {};
    fred['10'] = 1;
    // alert(fred.length);  // won't work anymore
</script>

On the other hand no extra entries will be generated and the access to a specific value works almost the same way as in an array. 另一方面,不会生成额外的条目,并且对特定值的访问与数组中的工作方式几乎相同。 If you want to traverse all values, use the following: 如果要遍历所有值,请使用以下命令:

for( var el in fred ) {
  if ( fred.hasOwnProperty( el ) ) {
    alert( fred[ el ] );
  }
}

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

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