Find Function and find index
Find function helps you to matches an array it automatically get break when he find the first number
in this case 3 is first no.
The find()
method returns the value of the first element in an array that pass a test (provided as a function).
The find() method executes the function once for each element present in the array:
- If it finds an array element where the function returns a true value, find() returns the value of that array element (and does not check the remaining values)
- Otherwise it returns undefined
Note: find() does not execute the function for empty arrays.
Note: find() does not change the original array.
let a = [12,3,4,5,5];
let number = a.find(function(num){
return num<10;
})
console.log(number);
findindex ;-
The findIndex()
method returns the index of the first element in an array that pass a test (provided as a function).
The findIndex() method executes the function once for each element present in the array:
- If it finds an array element where the function returns a true value, findIndex() returns the index of that array element (and does not check the remaining values)
- Otherwise it returns -1
Note: findIndex() does not execute the function for array elements without values.
Note: findIndex() does not change the original array.
n this case 1 is first no.
let a = [12,3,4,5,5];
let number = a.findIndex(function(num){
return num<10;
})
in this case we will get the index of the number
Comments
Post a Comment