You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/05-data-types/05-array-methods/article.md
+44-11Lines changed: 44 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -234,12 +234,13 @@ Now let's cover methods that search in an array.
234
234
235
235
### indexOf/lastIndexOf and includes
236
236
237
-
The methods [arr.indexOf](mdn:js/Array/indexOf), [arr.lastIndexOf](mdn:js/Array/lastIndexOf)and [arr.includes](mdn:js/Array/includes) have the same syntax and do essentially the same as their string counterparts, but operate on items instead of characters:
237
+
The methods [arr.indexOf](mdn:js/Array/indexOf)and [arr.includes](mdn:js/Array/includes) have the similar syntax and do essentially the same as their string counterparts, but operate on items instead of characters:
238
238
239
239
-`arr.indexOf(item, from)` -- looks for `item` starting from index `from`, and returns the index where it was found, otherwise `-1`.
240
-
-`arr.lastIndexOf(item, from)` -- same, but looks for from right to left.
241
240
-`arr.includes(item, from)` -- looks for `item` starting from index `from`, returns `true` if found.
242
241
242
+
Usually these methods are used with only one argument: the `item` to search. By default, the search is from the beginning.
A minor, but noteworthy feature of `includes` is that it correctly handles `NaN`, unlike `indexOf`:
260
271
261
272
```js run
262
273
constarr= [NaN];
263
-
alert( arr.indexOf(NaN) ); // -1 (should be 0, but === equality doesn't work for NaN)
274
+
alert( arr.indexOf(NaN) ); // -1 (wrong, should be 0)
264
275
alert( arr.includes(NaN) );// true (correct)
265
276
```
277
+
That's because `includes` was added to JavaScript much later and uses the more up to date comparison algorithm internally.
278
+
````
266
279
267
-
### find and findIndex
280
+
### find and findIndex/findLastIndex
268
281
269
282
Imagine we have an array of objects. How do we find an object with the specific condition?
270
283
@@ -304,7 +317,26 @@ In real life arrays of objects is a common thing, so the `find` method is very u
304
317
305
318
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.
306
319
307
-
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 and `-1` is returned when 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.
321
+
322
+
The [arr.findLastIndex](mdn:js/Array/findLastIndex) method is like `findIndex`, but searches from right to left, similar to `lastIndexOf`.
@@ -389,6 +421,7 @@ Literally, all elements are converted to strings for comparisons. For strings, l
389
421
To use our own sorting order, we need to supply a function as the argument of `arr.sort()`.
390
422
391
423
The function should compare two arbitrary values and return:
424
+
392
425
```js
393
426
function compare(a, b) {
394
427
if (a > b) return 1; // if the first value is greater than the second
@@ -633,7 +666,6 @@ So it's advised to always specify the initial value.
633
666
634
667
The method [arr.reduceRight](mdn:js/Array/reduceRight) does the same, but goes from right to left.
635
668
636
-
637
669
## Array.isArray
638
670
639
671
Arrays do not form a separate language type. They are based on objects.
@@ -642,7 +674,7 @@ So `typeof` does not help to distinguish a plain object from an array:
642
674
643
675
```js run
644
676
alert(typeof {}); // object
645
-
alert(typeof []); // same
677
+
alert(typeof []); //object (same)
646
678
```
647
679
648
680
...But arrays are used so often that there's a special method for that: [Array.isArray(value)](mdn:js/Array/isArray). It returns `true` if the `value` is an array, and `false` otherwise.
@@ -733,7 +765,7 @@ A cheat sheet of array methods:
733
765
-`reduce/reduceRight(func, initial)` -- calculate a single value over the array by calling `func` for each element and passing an intermediate result between the calls.
734
766
735
767
- Additionally:
736
-
-`Array.isArray(arr)` checks `arr` for being an array.
768
+
-`Array.isArray(value)` checks `value` for being an array, if so returns `true`, otherwise `false`.
737
769
738
770
Please note that methods `sort`, `reverse` and `splice` modify the array itself.
739
771
@@ -746,6 +778,7 @@ These methods are the most used ones, they cover 99% of use cases. But there are
746
778
These methods behave sort of like `||` and `&&` operators: if `fn` returns a truthy value, `arr.some()` immediately returns `true` and stops iterating over the rest of items; if `fn` returns a falsy value, `arr.every()` immediately returns `false` and stops iterating over the rest of items as well.
747
779
748
780
We can use `every` to compare arrays:
781
+
749
782
```js run
750
783
functionarraysEqual(arr1, arr2) {
751
784
returnarr1.length===arr2.length&&arr1.every((value, index) => value === arr2[index]);
0 commit comments