IE 8 中的 Javascript 数组问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5659591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:56:49  来源:igfitidea点击:

Javascript Arrays In IE 8 issue

javascriptarraysmethodsinternet-explorer-8indexof

提问by Abhishek Sanghvi

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

据我所知,Javascript 中的数组只不过是方法和对象的组合。

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

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

I have used for(x in y_array)and then displayed the value.

我已经使用for(x in y_array)然后显示了该值。

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

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

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?

谁能帮帮我吗?

回答by KooiInc

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

您不是第一个混淆数组和对象的人。SO 应该包含此类问题的常见问题解答;)

Let's try to explain things:

让我们试着解释一下:

An arrayis 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 objectis 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' }

一个数组看起来像:[ 'first', 'second', 'third', ..., 'nth' ]
一个对象看起来像:{ 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]

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

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

一个对象是无序的,所以它没有第一个元素。您可以使用myObject.somekey或从中检索任何元素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] <<<
}

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

对于对象,您使用键和in运算符使用循环(确保您只使用该.hasOwnAttribute方法检索对象的用户定义属性):

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

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.

基本上,将数组视为一个带有抽屉的橱柜,每个抽屉都包含一个值。一个物体可以想象成一堆盒子,盖子上贴着贴纸,描述了盒子里的东西。从一个物体中取出一些东西,你会问:在 x 堆中是否有一个贴有标签 y 的盒子,如果有,里面是什么?从数组中检索某些内容时,您会问:请给我 drawer nr x 的内容

Now as to your question: the array you are retrieving values for with a for..inloop contains a user defined method, namely indexOf. Using the object style loop for it, the array is treated as object, and the indexOfkey (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.

现在关于您的问题:您使用for..in循环检索值的数组包含一个用户定义的方法,即indexOf. 使用对象样式循环,数组被视为对象,并且indexOf键(具有像function(){...}我打赌一样的值)也显示出来。IE 这就是为什么在迭代数组时最好使用带有数字索引的传统 for 循环。

Why is this only in IE? In modern browsers indexOfis a native method of the Arrayprototype, 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.

为什么这仅在 IE 中?在现代浏览器中indexOfArray原型的本机方法,并且不显示本机方法(除非您遍历它们的原型)。IE < 9 没有用于数组的本机 indexOf 方法。您在脚本中使用该方法的某个地方已作为用户定义的扩展添加到 Array 原型中。

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

您的问题的底线:不要for ... in用于遍历数组的值。

回答by Pavel Hodek

For arrays you should use this for loop:

对于数组,你应该使用这个 for 循环:

var y_array = [1,2,3,4];
for (var i = 0; i < y_array.length; i++) {
  var value = y_array[i];
  // do what you want
  alert(i + ': ' + value);
}

For objects (objects are like associative arrays - property: value) use this loop:

对于对象(对象就像关联数组 - 属性:值)使用这个循环:

var y_array = { prop_1 : "value a", prop_2: "value_2", prop_3: 333 }
for (var key in y_array) {
   var value = y_array[key];
   // do what you want
   alert(key + ': ' + value);
}

回答by webx

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.

如果您的 json 对象中没有值,例如 jsobObj = {}。然后你在 IE < 9 中的空对象旁边得到了 indexOf 原型函数。(我敢打赌,像函数(){...} 这样的值)也显示了。

Your can check a condition in side your for Loop. and skip that indexOf.

你可以在你的 for 循环中检查条件。并跳过那个 indexOf。

if(key =='indexOf'){continue;}

if(key =='indexOf'){继续;}

E.g :

例如:

var jsonObj = { key_1 : "value a", key_2: "value_2", key_3: 333 }
for (var key in y_array) {
if(key == 'indexOf'){continue;}           // check if the array contain indexOf 
   var value = y_array[key];
   // do what you want
   alert(key + ': ' + value);
}