for...in

von 2 Mitwirkenden:

Zusammenfassung

Die for...in Anweisung durchläuft die Eigenschaften/Attribute eines Objekts in willkürlicher Reihenfolge. Für jede einzele Eigenschaft können Anweisungen ausgeführt werden.

Syntax

for (variable in object) {...
}
variable
in jedem Schleifendurchlauf wird der Name einer Eigenschaft der variable zugewiesen.
object
Das Objekt dessen Eigenschaften / Attribute durchlaufen werden sollen.

Description

Eine for...in Schleife durchläuft nur die aufzählbaren Eigenschaften. Objekte die "built–in constructors" wie Array und Object abgeleitet werden, haben von Object.prototype und String.prototype geerbte nicht-durchzählbare Eigenschaften, wie String's indexOf() Methode oder Object's toString() Methode. Die Schleife durchläuft alle (zählbaren) "enumerable" Eigenschaften des Objekts selber und diejenigen die das Objekt vom Constructor seines prototypes geerbt hat (Eigenschaften näher am Objekt in der prototype Kette überschreiben die Eigenschaften des  Prototypes).

Deleted, added or modified properties

A for...in loop iterates over the properties of an object in an arbitrary order (see the delete operator for more on why one cannot depend on the seeming orderliness of iteration, at least in a cross-browser setting). If a property is modified in one iteration and then visited at a later time, its value in the loop is its value at that later time. A property that is deleted before it has been visited will not be visited later. Properties added to the object over which iteration is occurring may either be visited or omitted from iteration. In general it is best not to add, modify or remove properties from the object during iteration, other than the property currently being visited. There is no guarantee whether or not an added property will be visited, whether a modified property (other than the current one) will be visited before or after it is modified, or whether a deleted property will be visited before it is deleted.

Array iteration and for...in

Note: for..in should not be used to iterate over an Array where index order is important.

Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties. 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.

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 for...of loop) when iterating over arrays where the order of access is important.

Iterating over own properties only

If you only want to consider properties attached to the object itself, and not its prototypes, use getOwnPropertyNames or perform a hasOwnProperty check (propertyIsEnumerable can also be used). Alternatively, if you know there won't be any outside code interference, you can extend built-in prototypes with a check method.

Examples

The following function takes as its argument an object. It then iterates over all the object's enumerable properties and returns a string of the property names and their values.

var obj = {a:1, b:2, c:3};
    
for (var prop in obj) {
  console.log("o." + prop + " = " + obj[prop]);
}

// Output:
// "o.a = 1"
// "o.b = 2"
// "o.c = 3"

The following function illustrates the use of hasOwnProperty(): the inherited properties are not displayed.

var triangle = {a:1, b:2, c:3};

function ColoredTriangle() {
  this.color = "red";
}

ColoredTriangle.prototype = triangle;

var obj = new ColoredTriangle();

for (var prop in obj) {
  if( obj.hasOwnProperty( prop ) ) {
    console.log("o." + prop + " = " + obj[prop]);
  } 
}

// Output:
// "o.color = red"

Specifications

Specification Status Comment
ECMAScript 1st Edition. Standard Initial definition.
ECMAScript 5.1 (ECMA-262)
Die Definition von 'for...in statement' in dieser Spezifikation.
Standard  
ECMAScript 6 (ECMA-262)
Die Definition von 'for...in statement' in dieser Spezifikation.
Anwärter Empfehlung  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Ja) (Ja) (Ja) (Ja) (Ja)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Ja) (Ja) (Ja) (Ja) (Ja) (Ja)

See also

Schlagwörter des Dokuments und Mitwirkende

Mitwirkende an dieser Seite: fscholz, lupo72
Zuletzt aktualisiert von: fscholz,
Seitenleiste ausblenden