Skip to content

Commit 0b9bc2f

Browse files
authored
Merge pull request #3634 from nakhodkin/patch-4
Fix grammar and typos
2 parents 541b7f9 + c66bace commit 0b9bc2f

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

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

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Array methods
22

3-
Arrays provide a lot of methods. To make things easier, in this chapter they are split into groups.
3+
Arrays provide a lot of methods. To make things easier, in this chapter, they are split into groups.
44

55
## Add/remove items
66

@@ -32,11 +32,11 @@ alert( arr.length ); // 3
3232

3333
The element was removed, but the array still has 3 elements, we can see that `arr.length == 3`.
3434

35-
That's natural, because `delete obj.key` removes a value by the `key`. It's all it does. Fine for objects. But for arrays we usually want the rest of elements to shift and occupy the freed place. We expect to have a shorter array now.
35+
That's natural, because `delete obj.key` removes a value by the `key`. It's all it does. Fine for objects. But for arrays we usually want the rest of the elements to shift and occupy the freed place. We expect to have a shorter array now.
3636

3737
So, special methods should be used.
3838

39-
The [arr.splice](mdn:js/Array/splice) method is a swiss army knife for arrays. It can do everything: insert, remove and replace elements.
39+
The [arr.splice](mdn:js/Array/splice) method is a Swiss army knife for arrays. It can do everything: insert, remove and replace elements.
4040

4141
The syntax is:
4242

@@ -62,7 +62,7 @@ alert( arr ); // ["I", "JavaScript"]
6262

6363
Easy, right? Starting from the index `1` it removed `1` element.
6464

65-
In the next example we remove 3 elements and replace them with the other two:
65+
In the next example, we remove 3 elements and replace them with the other two:
6666

6767
```js run
6868
let arr = [*!*"I", "study", "JavaScript",*/!* "right", "now"];
@@ -84,7 +84,7 @@ let removed = arr.splice(0, 2);
8484
alert( removed ); // "I", "study" <-- array of removed elements
8585
```
8686

87-
The `splice` method is also able to insert the elements without any removals. For that we need to set `deleteCount` to `0`:
87+
The `splice` method is also able to insert the elements without any removals. For that, we need to set `deleteCount` to `0`:
8888

8989
```js run
9090
let arr = ["I", "study", "JavaScript"];
@@ -114,7 +114,7 @@ alert( arr ); // 1,2,3,4,5
114114

115115
### slice
116116

117-
The method [arr.slice](mdn:js/Array/slice) is much simpler than similar-looking `arr.splice`.
117+
The method [arr.slice](mdn:js/Array/slice) is much simpler than the similar-looking `arr.splice`.
118118

119119
The syntax is:
120120

@@ -124,7 +124,7 @@ arr.slice([start], [end])
124124

125125
It returns a new array copying to it 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

127-
It's similar to a string method `str.slice`, but instead of substrings it makes subarrays.
127+
It's similar to a string method `str.slice`, but instead of substrings, it makes subarrays.
128128

129129
For instance:
130130

@@ -206,7 +206,7 @@ The [arr.forEach](mdn:js/Array/forEach) method allows to run a function for ever
206206
The syntax:
207207
```js
208208
arr.forEach(function(item, index, array) {
209-
// ... do something with item
209+
// ... do something with an item
210210
});
211211
```
212212

@@ -239,7 +239,7 @@ The methods [arr.indexOf](mdn:js/Array/indexOf) and [arr.includes](mdn:js/Array/
239239
- `arr.indexOf(item, from)` -- looks for `item` starting from index `from`, and returns the index where it was found, otherwise `-1`.
240240
- `arr.includes(item, from)` -- looks for `item` starting from index `from`, returns `true` if found.
241241

242-
Usually these methods are used with only one argument: the `item` to search. By default, the search is from the beginning.
242+
Usually, these methods are used with only one argument: the `item` to search. By default, the search is from the beginning.
243243

244244
For instance:
245245

@@ -255,7 +255,7 @@ alert( arr.includes(1) ); // true
255255

256256
Please note that `indexOf` uses the strict equality `===` for comparison. So, if we look for `false`, it finds exactly `false` and not the zero.
257257

258-
If we want to check if `item` exists in the array, and don't need the index, then `arr.includes` is preferred.
258+
If we want to check if `item` exists in the array and don't need the index, then `arr.includes` is preferred.
259259

260260
The method [arr.lastIndexOf](mdn:js/Array/lastIndexOf) is the same as `indexOf`, but looks for from right to left.
261261

@@ -274,12 +274,12 @@ const arr = [NaN];
274274
alert( arr.indexOf(NaN) ); // -1 (wrong, should be 0)
275275
alert( arr.includes(NaN) );// true (correct)
276276
```
277-
That's because `includes` was added to JavaScript much later and uses the more up to date comparison algorithm internally.
277+
That's because `includes` was added to JavaScript much later and uses the more up-to-date comparison algorithm internally.
278278
````
279279
280280
### find and findIndex/findLastIndex
281281
282-
Imagine we have an array of objects. How do we find an object with the specific condition?
282+
Imagine we have an array of objects. How do we find an object with a specific condition?
283283
284284
Here the [arr.find(fn)](mdn:js/Array/find) method comes in handy.
285285
@@ -297,7 +297,7 @@ The function is called for elements of the array, one after another:
297297
- `index` is its index.
298298
- `array` is the array itself.
299299
300-
If it returns `true`, the search is stopped, the `item` is returned. If nothing found, `undefined` is returned.
300+
If it returns `true`, the search is stopped, the `item` is returned. If nothing is found, `undefined` is returned.
301301
302302
For example, we have an array of users, each with the fields `id` and `name`. Let's find the one with `id == 1`:
303303
@@ -313,11 +313,11 @@ let user = users.find(item => item.id == 1);
313313
alert(user.name); // John
314314
```
315315
316-
In real life arrays of objects is a common thing, so the `find` method is very useful.
316+
In real life, arrays of objects are a common thing, so the `find` method is very useful.
317317
318318
Note that in the example we provide to `find` the function `item => item.id == 1` with one argument. That's typical, other arguments of this function are rarely used.
319319
320-
The [arr.findIndex](mdn:js/Array/findIndex) method has the same syntax, but returns the index where the element was found instead of the element itself. The value of `-1` is returned if nothing is found.
320+
The [arr.findIndex](mdn:js/Array/findIndex) method has the same syntax but returns the index where the element was found instead of the element itself. The value of `-1` is returned if nothing is found.
321321
322322
The [arr.findLastIndex](mdn:js/Array/findLastIndex) method is like `findIndex`, but searches from right to left, similar to `lastIndexOf`.
323323
@@ -450,11 +450,11 @@ alert(arr); // *!*1, 2, 15*/!*
450450
451451
Now it works as intended.
452452
453-
Let's step aside and think what's happening. The `arr` can be array of anything, right? It may contain numbers or strings or objects or whatever. We have a set of *some items*. To sort it, we need an *ordering function* that knows how to compare its elements. The default is a string order.
453+
Let's step aside and think about what's happening. The `arr` can be an array of anything, right? It may contain numbers or strings or objects or whatever. We have a set of *some items*. To sort it, we need an *ordering function* that knows how to compare its elements. The default is a string order.
454454
455455
The `arr.sort(fn)` method implements a generic sorting algorithm. We don't need to care how it internally works (an optimized [quicksort](https://en.wikipedia.org/wiki/Quicksort) or [Timsort](https://en.wikipedia.org/wiki/Timsort) most of the time). It will walk the array, compare its elements using the provided function and reorder them, all we need is to provide the `fn` which does the comparison.
456456
457-
By the way, if we ever want to know which elements are compared -- nothing prevents from alerting them:
457+
By the way, if we ever want to know which elements are compared -- nothing prevents us from alerting them:
458458
459459
```js run
460460
[1, -2, 15, 2, 0, 8].sort(function(a, b) {
@@ -526,7 +526,7 @@ Here's the situation from real life. We are writing a messaging app, and the per
526526
527527
The [str.split(delim)](mdn:js/String/split) method does exactly that. It splits the string into an array by the given delimiter `delim`.
528528
529-
In the example below, we split by a comma followed by space:
529+
In the example below, we split by a comma followed by a space:
530530
531531
```js run
532532
let names = 'Bilbo, Gandalf, Nazgul';
@@ -593,9 +593,9 @@ Arguments:
593593
- `index` -- is its position.
594594
- `array` -- is the array.
595595

596-
As function is applied, the result of the previous function call is passed to the next one as the first argument.
596+
As the function is applied, the result of the previous function call is passed to the next one as the first argument.
597597

598-
So, the first argument is essentially the accumulator that stores the combined result of all previous executions. And at the end it becomes the result of `reduce`.
598+
So, the first argument is essentially the accumulator that stores the combined result of all previous executions. And at the end, it becomes the result of `reduce`.
599599

600600
Sounds complicated?
601601

@@ -664,7 +664,7 @@ arr.reduce((sum, current) => sum + current);
664664

665665
So it's advised to always specify the initial value.
666666

667-
The method [arr.reduceRight](mdn:js/Array/reduceRight) does the same, but goes from right to left.
667+
The method [arr.reduceRight](mdn:js/Array/reduceRight) does the same but goes from right to left.
668668

669669
## Array.isArray
670670

@@ -689,7 +689,7 @@ alert(Array.isArray([])); // true
689689

690690
Almost all array methods that call functions -- like `find`, `filter`, `map`, with a notable exception of `sort`, accept an optional additional parameter `thisArg`.
691691

692-
That parameter is not explained in the sections above, because it's rarely used. But for completeness we have to cover it.
692+
That parameter is not explained in the sections above, because it's rarely used. But for completeness, we have to cover it.
693693

694694
Here's the full syntax of these methods:
695695

@@ -749,7 +749,7 @@ A cheat sheet of array methods:
749749
- `concat(...items)` -- returns a new array: copies all members of the current one and adds `items` to it. If any of `items` is an array, then its elements are taken.
750750

751751
- To search among elements:
752-
- `indexOf/lastIndexOf(item, pos)` -- look for `item` starting from position `pos`, return the index or `-1` if not found.
752+
- `indexOf/lastIndexOf(item, pos)` -- look for `item` starting from position `pos`, and return the index or `-1` if not found.
753753
- `includes(value)` -- returns `true` if the array has `value`, otherwise `false`.
754754
- `find/filter(func)` -- filter elements through the function, return first/all values that make it return `true`.
755755
- `findIndex` is like `find`, but returns the index instead of a value.
@@ -795,7 +795,7 @@ These methods are the most used ones, they cover 99% of use cases. But there are
795795

796796
For the full list, see the [manual](mdn:js/Array).
797797

798-
From the first sight it may seem that there are so many methods, quite difficult to remember. But actually that's much easier.
798+
At first sight, it may seem that there are so many methods, quite difficult to remember. But actually, that's much easier.
799799

800800
Look through the cheat sheet just to be aware of them. Then solve the tasks of this chapter to practice, so that you have experience with array methods.
801801

0 commit comments

Comments
 (0)