寻找其他职位的答案之后,我觉得我要问这个。我看着在JavaScript中 array.contains(OBJ)和找到一个JavaScript数组中的项目最好的方法? 并不能得到code那里工作。
After searching for an answer in other posts, I felt I have to ask this. I looked at array.contains(obj) in JavaScript and Best way to find an item in a JavaScript array? and couldn't get the code there to work.
我捕捉HTML嵌入code到一个数组,所以每个产品的html code的一条线。
I am capturing an html embed code into an array, so each item is a line of html code.
例如:
i[0]='<param name=\"bgcolor\" value=\"#FFFFFF\" />' i[1]='<param name=\"width" value=\"640\" />' i[2]='<param name=\"height\" value=\"360\" />' i[3]='more html code'我要搜索这个数组并找到行这里所说的'高度'的。
I want to search this array and find the line where the word 'height' is.
所以,理想情况下,我想编写一个返回,其中高度出现在项目的索引功能。
So ideally, I'd like to write a function that returns the index of the item where 'height' appears.
幸运的是,这个数组中没有重复,所以只出现一次。
Fortunately, there are no duplicates in this array, so there's only one occurrence.
用简单的搜索对象,我得到'假'的回报。
with simple object search I get 'false' returns.
任何想法?
推荐答案这是为遍历数组和寻找的正则表达式简单
It's as simple as iterating the array and looking for the regexp
function searchStringInArray (str, strArray) { for (var j=0; j<strArray.length; j++) { if (strArray[j].match(str)) return j; } return -1; }修改 - 使 STR 作为参数传递给函数
Edit - make str as an argument to function.
更多推荐
如何搜索字符串的字符串数组内
发布评论