Is .findIndex() a Higher Order Function in JavaScript?

a 夏天 提交于 2021-02-08 10:55:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!