Add missing terminators + other corrections (#23502)
* Add missing terminators + other corrections Missing terminators were added and for loop formatting was corrected. Even though terminators are optional, they can often confuse beginners. Also, a typo and a grammatical error was corrected. * fix: formatting
This commit is contained in:
@ -1,11 +1,12 @@
|
|||||||
---
|
---
|
||||||
title: For...In Loop
|
title: For...In Loop
|
||||||
---
|
---
|
||||||
The `for...in` statement iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.
|
The `for...in` statement iterates over the enumerable properties of an object, in an arbitrary order. For each distinct property, statements can be executed.
|
||||||
|
```js
|
||||||
for (variable in object) {
|
for (variable in object) {
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
| Required/Optional | Parameter | Description |
|
| Required/Optional | Parameter | Description |
|
||||||
|-------------------|-----------|----------------------------------------------------------------------|
|
|-------------------|-----------|----------------------------------------------------------------------|
|
||||||
@ -15,57 +16,57 @@ The `for...in` statement iterates over the enumerable properties of an object, i
|
|||||||
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
```js
|
||||||
|
// Initialize object.
|
||||||
|
var a = { "a": "Athens", "b": "Belgrade", "c": "Cairo" };
|
||||||
|
|
||||||
// Initialize object.
|
// Iterate over the properties.
|
||||||
a = { "a": "Athens", "b": "Belgrade", "c": "Cairo" }
|
var s = "";
|
||||||
|
for (var key in a) {
|
||||||
|
s += key + ": " + a[key];
|
||||||
|
s += "<br />";
|
||||||
|
}
|
||||||
|
document.write (s);
|
||||||
|
|
||||||
// Iterate over the properties.
|
// Output:
|
||||||
var s = ""
|
// a: Athens
|
||||||
for (var key in a) {
|
// b: Belgrade
|
||||||
s += key + ": " + a[key];
|
// c: Cairo
|
||||||
s += "<br />";
|
|
||||||
}
|
|
||||||
document.write (s);
|
|
||||||
|
|
||||||
// Output:
|
// Initialize the array.
|
||||||
// a: Athens
|
var arr = new Array("zero", "one", "two");
|
||||||
// b: Belgrade
|
|
||||||
// c: Cairo
|
|
||||||
|
|
||||||
// Initialize the array.
|
// Add a few expando properties to the array.
|
||||||
var arr = new Array("zero", "one", "two");
|
arr["orange"] = "fruit";
|
||||||
|
arr["carrot"] = "vegetable";
|
||||||
|
|
||||||
// Add a few expando properties to the array.
|
// Iterate over the properties and elements.
|
||||||
arr["orange"] = "fruit";
|
var s = "";
|
||||||
arr["carrot"] = "vegetable";
|
for (var key in arr) {
|
||||||
|
s += key + ": " + arr[key];
|
||||||
|
s += "<br />";
|
||||||
|
}
|
||||||
|
|
||||||
// Iterate over the properties and elements.
|
document.write (s);
|
||||||
var s = "";
|
|
||||||
for (var key in arr) {
|
|
||||||
s += key + ": " + arr[key];
|
|
||||||
s += "<br />";
|
|
||||||
}
|
|
||||||
|
|
||||||
document.write (s);
|
// Output:
|
||||||
|
// 0: zero
|
||||||
|
// 1: one
|
||||||
|
// 2: two
|
||||||
|
// orange: fruit
|
||||||
|
// carrot: vegetable
|
||||||
|
|
||||||
// Output:
|
// Efficient way of getting an object's keys using an expression within the for-in loop's conditions
|
||||||
// 0: zero
|
var myObj = {a: 1, b: 2, c:3}, myKeys = [], i=0;
|
||||||
// 1: one
|
for (myKeys[i++] in myObj);
|
||||||
// 2: two
|
|
||||||
// orange: fruit
|
|
||||||
// carrot: vegetable
|
|
||||||
|
|
||||||
// Efficient way of getting an object's keys using an expression within the for-in loop's conditions
|
document.write(myKeys);
|
||||||
var myObj = {a: 1, b: 2, c:3}, myKeys = [], i=0;
|
|
||||||
for (myKeys[i++] in myObj);
|
|
||||||
|
|
||||||
document.write(myKeys);
|
//Output:
|
||||||
|
// a
|
||||||
//Output:
|
// b
|
||||||
// a
|
// c
|
||||||
// b
|
```
|
||||||
// c
|
## Other Resources:
|
||||||
|
|
||||||
# Ohter Resources:
|
|
||||||
* [MDN link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in)
|
* [MDN link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in)
|
||||||
* [MSDN link](https://msdn.microsoft.com/library/55wb2d34.aspx)
|
* [MSDN link](https://msdn.microsoft.com/library/55wb2d34.aspx)
|
||||||
|
Reference in New Issue
Block a user