// findIndex(callback fn)
// .... return index (when condition meets)
// .... return -1 (if condition not meets)
const array = [5, 12, 8, 130, 44];
/// it returns the index of number which satisfy the condition true
const index = array.findIndex((item)=> item>10); //1
/// now we can check what element at that index...
console.log(array[index]); // array[1]
// find index of array item; test values using a function
const arr = ['A','B','C'];
const index = arr.findIndex((item) => item === 'B'); // index == 1
// .findIndex only accepts a callback function.
// returns the index of first value for which the function is true
const cities = [
"Orlando",
"Dubai",
"Edinburgh",
"Chennai",
"Denver",
"Eskisehir",
"Medellin",
"Yokohama",
"Denver",
];
const findIndexOfCity = (element) => {
return element === "Accra" || element === "Yokohama";
};
console.log(cities.findIndex(findIndexOfCity));
// Expected output is 7
//callback functions can take up to 3 arguments.
//The 1st argument, regardless of what you name it, is always
//assigned the element being checked. The 2nd argument, optional,
//is the index of this element. The 3rd argument, also optional,
//is the entire array.
const findIndexOfLastCity = (element, index) => {
return element === "Denver" && index > 4;
};
console.log(cities.findIndex(findIndexOfLastCity));
//expected output is 8 (skipped "Denver" at index 4)