From d9ee6e3c23b59eff2e996654dcfa8cfc9b86f32d Mon Sep 17 00:00:00 2001 From: Mojtaba Javan Date: Thu, 27 Dec 2018 06:29:22 +0330 Subject: [PATCH] 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`. --- .../05-data-types/05-array-methods/article.md | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/1-js/05-data-types/05-array-methods/article.md b/1-js/05-data-types/05-array-methods/article.md index c712a220..58544185 100644 --- a/1-js/05-data-types/05-array-methods/article.md +++ b/1-js/05-data-types/05-array-methods/article.md @@ -201,6 +201,35 @@ let arrayLike = { 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 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. - -## 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 Arrays do not form a separate language type. They are based on objects.