Skip to content

Commit 1ab0921

Browse files
committed
Add Array find and findIndex methods of ES6
1 parent 7fb8513 commit 1ab0921

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
6464
|21 | [Reflect](#reflect) |
6565
|22 | [Binary and Octal](#binary-and-octal) |
6666
|23 | [Proper Tail calls](#proper-tail-calls)|
67+
|24 | [Array find methods](#array-find-methods)
6768
| | **ES2016 Or ES7** |
6869
|1 | [Array includes](#array-includes) |
6970
|2 | [Exponentiation Operator](#exponentiation-operator) |
@@ -1088,6 +1089,34 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
10881089
console.log(factorial(1000));
10891090
console.log(factorial(10000));
10901091
```
1092+
24. ### Array find methods
1093+
ES6 introduced few array methods and two of them are `Array.find()` and `Array.findIndex()`.
1094+
1095+
**Array.find()**
1096+
This method returns the value of the first element in an array that satisfies the given test. Let's take an example of array with all even elements except one element and use `find` method to find the odd element.
1097+
1098+
```js
1099+
let arr = [2, 4, 5, 6, 8, 10];
1100+
1101+
function isOdd(i) {
1102+
return i % 2 !== 0;
1103+
}
1104+
1105+
console.log(arr.find(isOdd)); // 5
1106+
```
1107+
**Array.findIndex()**
1108+
1109+
This method returns the index of the first element in the array that satisfies the given test. Let's take an example of array with all even elements except one element and use `findIndex` method to find the index of odd element.
1110+
1111+
```js
1112+
let arr = [2, 4, 5, 6, 8, 10];
1113+
1114+
function isOdd(i) {
1115+
return i % 2 !== 0;
1116+
}
1117+
1118+
console.log(arr.findIndex(isOdd)); //2
1119+
```
10911120
10921121
**[⬆ Back to Top](#table-of-contents)**
10931122

0 commit comments

Comments
 (0)