【发布时间】:2014-10-11 10:27:10
【问题描述】:
我有字符串和字符串数组。
var strFruit = "Apple is good for health";
var arrayFruit = ["Apple is good for health"];
var strResult = strFruit.indexOf("Apple"); //strResult shows 0
var arrayResult = arrayFruit.indexOf("Apple"); // arrayResult shows -1
但如果我使用arrayFruit.indexOf("Apple is good for health") arrayResult 显示 0。
我的问题是为什么 indexOf 在 Array 元素中寻找完全匹配而不是在字符串中 以及两者的搜索有何不同?
PS:问这个问题的主要原因是我无法理解indexOf 的源代码。我可以理解它的作用(indexOf)什么字符串和数组。但我不确定 如何 使用字符串和数组?(poly fills or source code)。
【问题讨论】:
标签: javascript jquery arrays string indexof