开发者

What is the difference between 'for...in' and 'for each...in' in javascript? [duplicate]

This question already has answers here: What is the difference between for..in and for each..in in javascript? (4 answers) 开发者_JS百科 Closed 8 years ago.

I know in C# you use foreach like this: foreach string str in array1 {console.writeline(str);} Is "for each ... in" used like that in JavaScript too?


Afaik only Firefox supportes for each...in. It is not part of any standard and most tools like JSLint will throw errors if they encountered it.

The difference is that for...in loops over the keys of an object whereas for each...in loops over the values.

So the recommended way is to not use for each and do

for (var key in obj) {
    console.log(obj[key]);
}

instead.

Update: I cannot say how it relates to C#. But regarding arrays: Never use for...in to traverse arrays (yellow box here):

Although it may be tempting to use this as a way to iterate over an Array, this is a bad idea. The for...in statement iterates over user-defined properties in addition to the array elements, so if you modify the array's non-integer or non-positive properties (e.g. by adding a "foo" property to it or even by adding a method or property to Array.prototype), the for...in statement will return the name of your user-defined properties in addition to the numeric indexes.

Use a normal for loop instead:

for(var i = 0, l = arr.length; i<l; i++)

or more fancy:

for(var i = arr.length; i--;)


There is no for each in in standard JavaScript. It may exist in specific implementations, if they want to extend the language.

for..in in JavaScript only relates to arrays by association, because it relates to objects, and arrays are objects. for..in does not loop through array entries, as for each does in C#. Nor does it loop through array indexes although people tend to think it does. (It loops through property names; array indexes are property names, but an array object can have other properties as well.) It's more like for each with the keys of a dictionary. I've discussed this fairly thoroughly in this article.


"for each ... in" returns the values rather than the keys... See here:

http://www.dustindiaz.com/for-each-in/


The in operator is used to enumerate the properties (the names, not the values) of a javascript object - and in javascript, all non-primitives are objects (inherit from Object).

You use it like this

var obj = {
    a: "aaa",
    b: "bbb"
};

for (var key in obj) {
    if (obj.hasOwnProperty(key)){
        console.log("obj has a property " + key + ", with a value of " + obj[key]);
    }
}

Since in also enumerates properties inherited through the prototype chain, we must use hasOwnProperty to check if these are indeed the objects own properties, and not just inherited ones.

Note that the in operator should never be used to enumerate the items stored in arrays - for this you should use a regular for/while loop using the index.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜