Difference Between indexOf and findIndex function of array

后端 未结 7 591
暗喜
暗喜 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:28

    You can also use includes:

    [1, 2, 3].includes(2);      // true
    [1, 2, 3].includes(4);      // false
    [1, 2, 3].includes(3, 3);   // false
    

    but I prefer the indexOf method:

    var vals = [ "foo", "bar", 42, "baz" ];
    if (~vals.indexOf( 42 )) {
      // found it!
    }
    

提交回复
热议问题