简体   繁体   English

javascript for-in循环

[英]javascript for-in loop

As the defination says for-in loop is used to loop through the properties of an object ,than why is it looping the element of an array? 正如定义所指出的,for-in循环用于遍历对象的属性,而不是为什么循环数组的元素?

var arr = ['a','b','c'], indexes = [];

 Array.prototype.each = function() {/*blah*/};

 for (var index in arr) {
  indexes.push(index);
}
indexes; //["0", "1", "2", "each"]

why are 0,1,2 enumerated?They are not the properties 为什么要枚举0,1,2?不是属性

Quote from the documentation : documentation引用:

for..in should not be used to iterate over an Array where index order is important. for..in不应用于遍历索引顺序很重要的Array。 Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties. 数组索引只是具有整数名称的可枚举属性,其他方面与常规Object属性相同。 There is no guarantee that for...in will return the indexes in any particular order and it will return all enumerable properties, including those with non–integer names and those that are inherited. 无法保证for ... in将以任何特定顺序返回索引,并且将返回所有可枚举的属性,包括具有非整数名称的属性和被继承的属性。

Because the order of iteration is implementation dependent, iterating over an array may not visit elements in a consistent order. 因为迭代的顺序取决于实现,所以在数组上进行迭代可能不会以一致的顺序访问元素。 Therefore it is better to use a for loop with a numeric index (or Array.forEach or the non-standard for...of loop) when iterating over arrays where the order of access is important. 因此,在访问顺序很重要的数组上进行迭代时,最好使用带有数字索引的for循环(或Array.forEach或非标准的for ... of循环)。

The key here holding the answer to your question is the following sentence: 掌握您问题答案的关键是以下句子:

Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties. 数组索引只是具有整数名称的可枚举属性,其他方面与常规Object属性相同。

And the following sentence sums it up: 以下句子总结了一下:

for..in should not be used to iterate over an Array where index order is important. for..in不应用于遍历索引顺序很重要的Array。

Each index in the array for which the array has an element is a property of that array. 数组中具有数组元素的每个索引都是该数组的属性。 So this is basically what your array looks like behind the scenes: 因此,这基本上就是您的阵列在幕后的样子:

>>> arr

    {
        0: 'a',
        1: 'b',
        2: 'c',
        'each': function() {}
        'length': 3
    };

These keys are enumerable which is the reason why you're seeing them in your output. 这些键是可枚举的,这就是为什么您在输出中看到它们的原因。

The for in loop iterates over keys, not values. for in循环遍历键,而不是值。 So it's giving your the array indexes 0, 1, 2 not the values. 因此,它给您的数组索引0、1、2而不是值。

You could do it like this but it's bad practice to use a for in on an array. 您可以这样做,但是for in数组上使用for in是不好的做法。

for (var index in arr) {
    indexes.push(arr[index]);
}

You should use a regular for loop 您应该使用常规的for循环

for (var i = 0; i < arr.length; i++) {
    indexes.push(arr[i]);
}

for...in interates over the enumerable properties of an Object. for...in遍历对象的可枚举属性。 The enumerable property of Array is the index. Array的可枚举属性是索引。

More information can be found here . 可以在此处找到更多信息。

AMCAScript 6 defines the for...of operator which allows iteration over the values. AMCAScript 6定义了for...of运算符 ,该运算符允许对值进行迭代。 However this has not yet been adopted. 但是,这尚未被采纳。

Yes, if you really want to use for in and not use the keys you can make the values into keys like this eg: 是的,如果您真的想使用in而不使用键,则可以将值变成这样的键,例如:

var arr = {'a':1,'b':1,'c':1};
for(var index in arr)
indexes.push(index);

same as setting arr['a']=1 etc. It's true that for..in iterates over the keys - not the values. 与设置arr ['a'] = 1等相同。的确,for..in遍历键-而不是值。

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

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