Reordering sections
`forEach` is mentioned as known in **reduce/reduceRight** which is a subsection of **Transform an array**, and this section is above **Iterate: forEach** where we first learn about `forEach`.
This commit is contained in:
parent
4e5aef8b1b
commit
d9ee6e3c23
1 changed files with 29 additions and 29 deletions
|
@ -201,6 +201,35 @@ let arrayLike = {
|
||||||
alert( arr.concat(arrayLike) ); // 1,2,something,else
|
alert( arr.concat(arrayLike) ); // 1,2,something,else
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Iterate: forEach
|
||||||
|
|
||||||
|
The [arr.forEach](mdn:js/Array/forEach) method allows to run a function for every element of the array.
|
||||||
|
|
||||||
|
The syntax:
|
||||||
|
```js
|
||||||
|
arr.forEach(function(item, index, array) {
|
||||||
|
// ... do something with item
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
For instance, this shows each element of the array:
|
||||||
|
|
||||||
|
```js run
|
||||||
|
// for each element call alert
|
||||||
|
["Bilbo", "Gandalf", "Nazgul"].forEach(alert);
|
||||||
|
```
|
||||||
|
|
||||||
|
And this code is more elaborate about their positions in the target array:
|
||||||
|
|
||||||
|
```js run
|
||||||
|
["Bilbo", "Gandalf", "Nazgul"].forEach((item, index, array) => {
|
||||||
|
alert(`${item} is at index ${index} in ${array}`);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
The result of the function (if it returns any) is thrown away and ignored.
|
||||||
|
|
||||||
|
|
||||||
## Searching in array
|
## Searching in array
|
||||||
|
|
||||||
These are methods to search for something in an array.
|
These are methods to search for something in an array.
|
||||||
|
@ -584,35 +613,6 @@ So it's advised to always specify the initial value.
|
||||||
|
|
||||||
The method [arr.reduceRight](mdn:js/Array/reduceRight) does the same, but goes from right to left.
|
The method [arr.reduceRight](mdn:js/Array/reduceRight) does the same, but goes from right to left.
|
||||||
|
|
||||||
|
|
||||||
## Iterate: forEach
|
|
||||||
|
|
||||||
The [arr.forEach](mdn:js/Array/forEach) method allows to run a function for every element of the array.
|
|
||||||
|
|
||||||
The syntax:
|
|
||||||
```js
|
|
||||||
arr.forEach(function(item, index, array) {
|
|
||||||
// ... do something with item
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
For instance, this shows each element of the array:
|
|
||||||
|
|
||||||
```js run
|
|
||||||
// for each element call alert
|
|
||||||
["Bilbo", "Gandalf", "Nazgul"].forEach(alert);
|
|
||||||
```
|
|
||||||
|
|
||||||
And this code is more elaborate about their positions in the target array:
|
|
||||||
|
|
||||||
```js run
|
|
||||||
["Bilbo", "Gandalf", "Nazgul"].forEach((item, index, array) => {
|
|
||||||
alert(`${item} is at index ${index} in ${array}`);
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
The result of the function (if it returns any) is thrown away and ignored.
|
|
||||||
|
|
||||||
## Array.isArray
|
## Array.isArray
|
||||||
|
|
||||||
Arrays do not form a separate language type. They are based on objects.
|
Arrays do not form a separate language type. They are based on objects.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue