与此JavaScript语法有点混淆-> arr.indexOfsearchElement [,fromIndex]

问题描述

   const beasts = ['ant','bison','camel','duck','bison'];

console.log(beasts.indexOf('bison'));
// expected output: 1

// start from index 2
console.log(beasts.indexOf('bison',2));
// expected output: 4

console.log(beasts.indexOf('giraffe'));
// expected output: -1

我一直在反复研究这种js语法。我从https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf获得了这段代码 它说fromIndex参数是开始搜索的索引。现在,从上面的代码中,我对fromIndex参数的计数如何发生感到困惑。从代码行上:

// start from index 2
console.log(beasts.indexOf('bison',2));
// expected output: 4

骆驼是索引为2的项目。在预期输出为4的情况下,计数如何发生?

当我尝试更改第二个参数时,

// start from index 3
console.log(beasts.indexOf('bison',3)); 

Duck是索引为3的项目。但是indexOf('bison',3)的输出仍然为4。

计数实际上是如何发生的?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)