Difference Between indexOf and findIndex function of array

后端 未结 7 588
暗喜
暗喜 2020-11-30 19:42

I am confused between the difference between the two function indexOf and find Index in an array.

The documentation says

findIndex - Returns

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 20:18

    The main difference are the parameters of these functions:

    • Array.prototype.indexOf() expects a value as first parameter. This makes it a good choice to find the index in arrays of primitive types (like string, number, or boolean).

    • Array.prototype.findIndex() expects a callback as first parameter. Use this if you need the index in arrays with non-primitive types (e.g. objects) or your find condition is more complex than just a value.

    See the links for examples of both cases.

提交回复
热议问题