// .find method only accepts a callback function
// and returns the first value for which the function is true.
const cities = [
"Orlando",
"Dubai",
"Denver",
"Edinburgh",
"Chennai",
"Accra",
"Denver",
"Eskisehir",
];
const findCity = (element) => {
return element === "Denver";
};
console.log(cities.find(findCity));
//Expected output is 4 (first instance of "Denver" in array)
var ages = [3, 10, 18, 20];
function checkAdult(age) {
return age >= 18;
}
/* find() runs the input function agenst all array components
till the function returns a value
*/
ages.find(checkAdult);
const list = [5, 12, 8, 130, 44];
// a find metóduson belül a number egy paraméter
// azért nem szükséges köré a zárójel, mert csak egy paramétert adunk át
// minden más esetben így kell írni:
// const found = list.find((index, number) => number > index);
const found = list.find(number => number > 10);
console.log(found);
// --> 12
The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
//find is higher oder function that return only if condition is true
const names= ["Shirshak","SabTech","Kdc"]
const searchName = names.find(function(name){
return names.inclues("Shirshak")
})
str.indexOf("locate"); // return location of first find value
str.lastIndexOf("locate"); // return location of last find value
str.indexOf("locate", 15); // start search from location 15 and then take first find value
str.search("locate");
//The search() method cannot take a second start position argument.
//The indexOf() method cannot take powerful search values (regular expressions).
/* This method returns first element of the array that satisfies the condition
specified in the callback function.
*/
const x = [1, 2, 3, 4, 5];
const y = x.find(el => el*2 === 2);
console.log("y is: ", y); // y is: 1
/* Now, if you see the output of the above example, the value of y is 1.
This is because the find() method searches for first element in the array
that satisfies the condition specified.
*/