问题
Basically I understand that .filter()
, .reduce()
, and .map()
are higher-order functions because they take other functions as their arguments, like this:
arrayOfWords.filter(words => words.length > 6);
unflattenedArray.reduce((accumulator, currentValue) => accumulator + currentValue);
arrayOfIntegers.map(x => x * 2);
So then is .findIndex()
also a higher-order function?
Seems to operate just like the others:
let fruits = ["apple", "banana", "cantaloupe", "blueberries", "grapefruit"];
let index = fruits.findIndex(fruit => fruit === "blueberries");
回答1:
Yes, that is correct, findIndex
is a higher-order function.
The findIndex()
method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating no element passed the test.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
来源:https://stackoverflow.com/questions/55913179/is-findindex-a-higher-order-function-in-javascript