IE 8 中的 Javascript 数组问题
据我所知,Javascript中的数组只不过是方法和对象的组合。
现在我的任务是显示数组的值(例如y_array
),
我使用了for(x in y_array)
,然后显示了该值。
在mozilla和IE中它工作正常,但在IE中它显示数组的第一个元素,索引为indexOf
,值为indexOf(obj, from)
,这是我不想要的。
我尝试过
if(x!='indexOf') { display the array value ; }
它有效,一切都很好,但是显示了数组的广泛使用,我正在寻找一些永久的修复程序,而不是这个硬编码的修复程序。
有人可以帮我吗?
As per what I know, arrays in Javascript is nothing but the combination of methods and objects.
Now my task is to display the values of array (say y_array
)
I have used for(x in y_array)
and then displayed the value.
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.
I tried
if(x!='indexOf') { display the array value ; }
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.
Can anyone please help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不是第一个混淆数组和对象的人。 SO 应该包含此类问题的常见问题解答;)
让我们尝试解释一下:
数组是一行值,可以使用它们在行中的位置来检索。数组值的顺序是固定的(并且可以重新排序)。
对象是一个变量,包含键值对形式的命名属性。属于对象的键值对的顺序是任意的。
数组如下所示:
[ 'first', 'second', 'third', ..., 'nth' ]
对象如下所示:
{ first:'firstvalue', secondary:'secondvalue', ..., nth:'nthvalue' }
数组的第一个元素是索引为 0 的元素(因此该行中的第一个位置的索引值为 0)。您可以使用
myArray[0]
检索它。对象是无序的,因此它没有第一个元素。您可以使用
myObject.somekey
或myObject['somekey']
从中检索任何元素。对于数组,您使用循环遍历编号索引,直到到达数组末尾:
对于对象,您使用使用键和
in
运算符的循环(确保您只检索用户定义的属性)对象的.hasOwnAttribute
方法):基本上,将数组视为带有抽屉的柜子,每个抽屉都包含一个值。一个物体可以想象为一堆盒子,盖子上贴着贴纸,描述盒子里的东西。从物体中取出某物时,您会问:在 x 堆中是否有一个带有贴纸 y 的盒子,如果有,里面是什么?从数组中检索某些内容时,您会问:请给我抽屉 nr x 的内容。
现在关于您的问题:您使用
for..in
循环检索值的数组包含一个用户定义的方法,即indexOf
。使用对象样式循环,数组被视为对象,并显示indexOf
键(其值类似于function(){...}
我打赌)也。这就是为什么在迭代数组时最好使用带有数字索引的传统 for 循环。为什么这只在 IE 中出现?在现代浏览器中,
indexOf
是Array
原型的本机方法,并且本机方法不会显示(除非您循环遍历它们的原型)。 IE < 9 没有用于数组的本机 indexOf 方法。在脚本中的某个位置,您使用的方法已作为用户定义的扩展添加到数组原型中。问题的底线:不要使用
for ... in
循环遍历数组的值。You are not the first mixing up arrays and objects. SO should contain a FAQ for this kind of questions ;)
Let's try to explain things:
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.
An array looks like:
[ 'first', 'second', 'third', ..., 'nth' ]
An object looks like:
{ first:'firstvalue', second:'secondvalue', ..., nth:'nthvalue' }
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]
An object is unordered, so it has no first element. You retrieve any element from it using
myObject.somekey
ormyObject['somekey']
.For arrays you use a loop iterating through the numbered index until the end of the array is reached:
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):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.
Now as to your question: the array you are retrieving values for with a
for..in
loop contains a user defined method, namelyindexOf
. Using the object style loop for it, the array is treated as object, and theindexOf
key (with value likefunction(){...}
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.Why is this only in IE? In modern browsers
indexOf
is a native method of theArray
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.对于数组,您应该使用此 for 循环:
对于对象(对象类似于关联数组 - 属性:值),请使用此循环:
For arrays you should use this for loop:
For objects (objects are like associative arrays - property: value) use this loop:
如果 json 对象中没有值,如 jsobObj = {}。然后你在IE中的空对象中得到了indexOf原型函数< 9.(我敢打赌,其值类似于 function(){...} )也显示了。
您可以检查 for 循环中的条件。并跳过那个indexOf。
if(key =='indexOf'){继续;}
例如:
if there is no value in your json Object like jsobObj = {}. Then you got the indexOf prototype function in side the empty object in IE < 9. (with value like function(){...} I bet) is shown too.
Your can check a condition in side your for Loop. and skip that indexOf.
if(key =='indexOf'){continue;}
E.g :