Skip to content

Commit 87bb908

Browse files
authored
Merge pull request #703 from mrmowji/patch-3
Minor improvements
2 parents 0cd59bf + 2cff951 commit 87bb908

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

1-js/05-data-types/05-array-methods/article.md

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ The syntax is:
122122
arr.slice(start, end)
123123
```
124124

125-
It returns a new array where it copies all items start index `"start"` to `"end"` (not including `"end"`). Both `start` and `end` can be negative, in that case position from array end is assumed.
125+
It returns a new array containing all items from index `"start"` to `"end"` (not including `"end"`). Both `start` and `end` can be negative, in that case position from array end is assumed.
126126

127127
It works like `str.slice`, but makes subarrays instead of substrings.
128128

@@ -201,6 +201,35 @@ let arrayLike = {
201201
alert( arr.concat(arrayLike) ); // 1,2,something,else
202202
```
203203

204+
## Iterate: forEach
205+
206+
The [arr.forEach](mdn:js/Array/forEach) method allows to run a function for every element of the array.
207+
208+
The syntax:
209+
```js
210+
arr.forEach(function(item, index, array) {
211+
// ... do something with item
212+
});
213+
```
214+
215+
For instance, this shows each element of the array:
216+
217+
```js run
218+
// for each element call alert
219+
["Bilbo", "Gandalf", "Nazgul"].forEach(alert);
220+
```
221+
222+
And this code is more elaborate about their positions in the target array:
223+
224+
```js run
225+
["Bilbo", "Gandalf", "Nazgul"].forEach((item, index, array) => {
226+
alert(`${item} is at index ${index} in ${array}`);
227+
});
228+
```
229+
230+
The result of the function (if it returns any) is thrown away and ignored.
231+
232+
204233
## Searching in array
205234

206235
These are methods to search for something in an array.
@@ -274,7 +303,7 @@ alert(user.name); // John
274303

275304
In real life arrays of objects is a common thing, so the `find` method is very useful.
276305

277-
Note that in the example we provide to `find` a single-argument function `item => item.id == 1`. Other parameters of `find` are rarely used.
306+
Note that in the example we provide to `find` the function `item => item.id == 1` with one argument. Other arguments of this function are rarely used.
278307

279308
The [arr.findIndex](mdn:js/Array/findIndex) method is essentially the same, but it returns the index where the element was found instead of the element itself.
280309

@@ -477,7 +506,7 @@ alert( str.split('') ); // t,e,s,t
477506
```
478507
````
479508

480-
The call [arr.join(str)](mdn:js/Array/join) does the reverse to `split`. It creates a string of `arr` items glued by `str` between them.
509+
The call [arr.join(separator)](mdn:js/Array/join) does the reverse to `split`. It creates a string of `arr` items glued by `separator` between them.
481510

482511
For instance:
483512

@@ -491,7 +520,7 @@ alert( str ); // Bilbo;Gandalf;Nazgul
491520

492521
### reduce/reduceRight
493522

494-
When we need to iterate over an array -- we can use `forEach`.
523+
When we need to iterate over an array -- we can use `forEach`, `for` or `for..of`.
495524

496525
When we need to iterate and return the data for each element -- we can use `map`.
497526

@@ -500,7 +529,7 @@ The methods [arr.reduce](mdn:js/Array/reduce) and [arr.reduceRight](mdn:js/Array
500529
The syntax is:
501530

502531
```js
503-
let value = arr.reduce(function(previousValue, item, index, arr) {
532+
let value = arr.reduce(function(previousValue, item, index, array) {
504533
// ...
505534
}, initial);
506535
```
@@ -509,7 +538,7 @@ The function is applied to the elements. You may notice the familiar arguments,
509538

510539
- `item` -- is the current array item.
511540
- `index` -- is its position.
512-
- `arr` -- is the array.
541+
- `array` -- is the array.
513542

514543
So far, like `forEach/map`. But there's one more argument:
515544

@@ -585,34 +614,6 @@ So it's advised to always specify the initial value.
585614
The method [arr.reduceRight](mdn:js/Array/reduceRight) does the same, but goes from right to left.
586615

587616

588-
## Iterate: forEach
589-
590-
The [arr.forEach](mdn:js/Array/forEach) method allows to run a function for every element of the array.
591-
592-
The syntax:
593-
```js
594-
arr.forEach(function(item, index, array) {
595-
// ... do something with item
596-
});
597-
```
598-
599-
For instance, this shows each element of the array:
600-
601-
```js run
602-
// for each element call alert
603-
["Bilbo", "Gandalf", "Nazgul"].forEach(alert);
604-
```
605-
606-
And this code is more elaborate about their positions in the target array:
607-
608-
```js run
609-
["Bilbo", "Gandalf", "Nazgul"].forEach((item, index, array) => {
610-
alert(`${item} is at index ${index} in ${array}`);
611-
});
612-
```
613-
614-
The result of the function (if it returns any) is thrown away and ignored.
615-
616617
## Array.isArray
617618

618619
Arrays do not form a separate language type. They are based on objects.
@@ -694,6 +695,9 @@ A cheatsheet of array methods:
694695
- `includes(value)` -- returns `true` if the array has `value`, otherwise `false`.
695696
- `find/filter(func)` -- filter elements through the function, return first/all values that make it return `true`.
696697
- `findIndex` is like `find`, but returns the index instead of a value.
698+
699+
- To iterate over elements:
700+
- `forEach(func)` -- calls `func` for every element, does not return anything.
697701

698702
- To transform the array:
699703
- `map(func)` -- creates a new array from results of calling `func` for every element.
@@ -702,9 +706,6 @@ A cheatsheet of array methods:
702706
- `split/join` -- convert a string to array and back.
703707
- `reduce(func, initial)` -- calculate a single value over the array by calling `func` for each element and passing an intermediate result between the calls.
704708

705-
- To iterate over elements:
706-
- `forEach(func)` -- calls `func` for every element, does not return anything.
707-
708709
- Additionally:
709710
- `Array.isArray(arr)` checks `arr` for being an array.
710711

0 commit comments

Comments
 (0)