Skip to content

Commit c3b904d

Browse files
committed
minor fixes
1 parent bebcbfa commit c3b904d

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

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

+42-13
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,13 @@ Now let's cover methods that search in an array.
234234

235235
### indexOf/lastIndexOf and includes
236236

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:
238238

239239
- `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.
241240
- `arr.includes(item, from)` -- looks for `item` starting from index `from`, returns `true` if found.
242241

242+
Usually these methods are used with only one argument: the `item` to search. By default, the search is from the beginning.
243+
243244
For instance:
244245

245246
```js run
@@ -249,27 +250,34 @@ alert( arr.indexOf(0) ); // 1
249250
alert( arr.indexOf(false) ); // 2
250251
alert( arr.indexOf(null) ); // -1
251252

252-
let fruits = ['Plum', 'Apple', 'Orange', 'Plum']
253-
// note that the lastIndexOf method looks for from the end, but index counted from beginning
254-
alert( fruits.lastIndexOf(Plum)) // 3
255-
256253
alert( arr.includes(1) ); // true
257254
```
258255

259-
Note that the methods use `===` comparison. So, if we look for `false`, it finds exactly `false` and not the zero.
256+
Please note that `indexOf` uses the strict equality `===` for comparison. So, if we look for `false`, it finds exactly `false` and not the zero.
257+
258+
If we want to check if `item` exists in the array, and don't need the exact index, then `arr.includes` is preferred.
259+
260+
The method [arr.lastIndexOf](mdn:js/Array/lastIndexOf) is the same as `indexOf`, but looks for from right to left.
261+
262+
```js run
263+
let fruits = ['Apple', 'Orange', 'Apple']
260264

261-
If we want to check for inclusion, and don't want to know the exact index, then `arr.includes` is preferred.
265+
alert( arr.indexOf('Apple') ); // 0 (first Apple)
266+
alert( arr.lastIndexOf('Apple') ); // 2 (last Apple)
267+
```
262268

263-
Also, a minor, but noteworthy feature of `includes` is that it correctly handles `NaN`, unlike `indexOf/lastIndexOf`:
269+
````smart header="The `includes` method handles `NaN` correctly"
270+
A minor, but noteworthy feature of `includes` is that it correctly handles `NaN`, unlike `indexOf`:
264271

265272
```js run
266273
const arr = [NaN];
267-
alert( arr.indexOf(NaN) ); // -1 (should be 0, but equality test === doesn't work for NaN)
274+
alert( arr.indexOf(NaN) ); // -1 (wrong, should be 0)
268275
alert( arr.includes(NaN) );// true (correct)
269276
```
277+
That's because `includes` was added to JavaScript much later and uses the more up to date comparison algorithm internally.
278+
````
270279
271-
272-
### find and findIndex
280+
### find and findIndex/findLastIndex
273281
274282
Imagine we have an array of objects. How do we find an object with the specific condition?
275283
@@ -309,7 +317,28 @@ In real life arrays of objects is a common thing, so the `find` method is very u
309317
310318
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.
311319
312-
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`.
323+
324+
Here's an example:
325+
326+
```js run
327+
let users = [
328+
{id: 1, name: "John"},
329+
{id: 2, name: "Pete"},
330+
{id: 3, name: "Mary"},
331+
{id: 4, name: "John"}
332+
];
333+
334+
// Find the index of the first John
335+
alert(users.findIndex(user => user.name == 'John')); // 0
336+
337+
// Find the index of the last John
338+
alert(users.findLastIndex(user => user.name == 'John')); // 3
339+
```
340+
341+
313342
314343
### filter
315344

0 commit comments

Comments
 (0)