JavaScript数组在IE 8的问题 [英] Javascript Arrays In IE 8 issue

查看:138
本文介绍了JavaScript数组在IE 8的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我所知道的,在Javascript阵列不过的方法和对象的组合。

As per what I know, arrays in Javascript is nothing but the combination of methods and objects.

现在我的任务就是显示数组的值(比如 y_array

Now my task is to display the values of array (say y_array)

我用为(以y_array X),然后显示的值。

在Mozilla和IE中它工作正常,但在IE中显示索引数组的第一个元素作为的indexOf 和值的indexOf( OBJ,从)这我不想要。

In mozilla and in IE its working fine, but in IE it displays the first element of array with index as indexOf and value is indexOf(obj, from) which i dont want.

我试过

if(x!='indexOf') {  display the array value ; }

它的工作和事情是好的,但也显示了广泛的使用数组,我寻找一些永久性的修复,而不是这个硬codeD之一。

It worked and things were fine but there is extensive use of arrays been displayed and I am looking for some permanent fix rather than this hardcoded one.

谁能帮帮我吗?

推荐答案

您不是第一个混淆数组和对象。所以要包含一个常见问题解答这类问题;)

You are not the first mixing up arrays and objects. SO should contain a FAQ for this kind of questions ;)

让我们尝试解释事情:

这是的阵列是值的一排,可以使用他们的行中的位置进行检索。数组值的次序是固定的(并且可以被重新排序)。

An array is a row of values, which can be retrieved using their position in the row. The order of the array values is fixed (and may be reordered).

对象是一个包含键 - 值对的形式命名属性的变量。属于一个对象的键 - 值对的顺序是任意的。

An object is a variable that contains named properties in the form of key-value pairs. The order of the key-value pairs belonging to an object is arbitrary.

这是数组如下: [第一,第二,第三,...,第n'] 结果
一个对象如下: {第一:'firstvalue,第二:secondvalue',...,n次:nthvalue'}

的阵列的第一个元素是索引为0(因此该行中的第一位置的索引值0)的元素。你使用它检索 myArray的[0]

The first element of an array is the element with index 0 (so the first position in the row has index value 0). You retrieve it using myArray[0]

一个目的是无序的,因此它没有一个元素。您可以使用从中检索的任何元素 myObject.somekey myObject的['somekey']

An object is unordered, so it has no first element. You retrieve any element from it using myObject.somekey or myObject['somekey'].

对于数组你使用遍历编号指数迭代,直到达到阵列的末尾:

For arrays you use a loop iterating through the numbered index until the end of the array is reached:

var i=0, len = myArray.length;
for ( i; i<len; i++ ) {
     //do something with >>> myArray[i] <<<
}

有关对象的使用使用键和 A回路操作符(确保你只检索与该对象的用户定义的属性 .hasOwnAttribute 法):

For objects you use a loop using the key and the in operator (making sure you are only retrieving user defined properties of the object with the .hasOwnAttribute method):

for ( var key in myObject ){
  if (myObject.hasOwnProperty(key)) {
     // do something with >>> myObject[key] <<<
  }
}

基本上,想到一个阵列作为带抽屉橱柜,每个包含一个值。一个对象可以被想象成箱一堆用盖子上的贴纸,描述框的内容。从对象检索的东西,你问:有与桩x和如果是这样的贴纸Ÿ一个盒子,里面是什么的?从一个数组中检索的东西,你问:请给我抽屉里的内容NR X

Basically, think of an array as a cupboard with drawers, each containing a value. An object can be imagined as a pile of boxes with stickers on the lid, describing the content of the box. Retrieving something from an object, you ask: is there a box with sticker y in pile x and if so, what's in it? Retrieving something from an array, you ask: please give me the contents of drawer nr x.

现在,作为对您的问题的:您是用的for..in 循环检索值的数组包含一个用户定义的方法,即的indexOf 。使用它的对象样式循环,数组被视为对象,而的indexOf 键(像值函数(){...} 我敢打赌)太中。 IE这就是为什么它可能是更好遍历数组时,使用传统的for循环以数字指标。

Now as to your question: the array you are retrieving values for with a for..in loop contains a user defined method, namely indexOf. Using the object style loop for it, the array is treated as object, and the indexOf key (with value like function(){...} I bet) is shown too. IE That's why it may be better to use a traditional for loop with a numeric index when iterating over arrays.

这是为什么只能在IE 的?在现代浏览器的indexOf 阵列原型的一个本地方法,而本地方法没有显示(除非你遍历他们的原型即是)。 IE&LT; 9不具有阵列本地的indexOf方法。在某处使用方法脚本已被添加到阵列的原型作为一个用户定义的扩展。

Why is this only in IE? In modern browsers indexOf is a native method of the Array prototype, and native methods are not shown (unless you loop through their prototype that is). IE < 9 doesn't have a native indexOf method for arrays. Somewhere in the scripting you use the method has been added to the Array prototype as a user defined extension.

您的问题底线:不使用为中... 通过一个数组的值环

Bottom line for your problem: don't use for ... in to loop through the values of an array.

这篇关于JavaScript数组在IE 8的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆