Difference Between indexOf and findIndex function of array

后端 未结 7 587
暗喜
暗喜 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:37

    The main difference are the parameters of these functions:

    -> Array.prototype.indexOf() :

       var fruits = ["Banana", "Orange", "Apple", "Mango"];
       var a = fruits.indexOf("Apple");
       The result of a will be: 2
    

    ->Array.prototype.findIndex() :

           var ages = [3, 10, 18, 20];
    
           function checkAdult(age) {
            return age >= 18;
           }
    
           function myFunction() {
             document.getElementById("demo").innerHTML = 
             ages.findIndex(checkAdult);
           }
    
           The result will be: 2
    
    0 讨论(0)
提交回复
热议问题