Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

javascript - 如何在JavaScript / jQuery中查找数组是否包含特定字符串? [重复](How to find if an array contains a specific string in JavaScript/jQuery? [duplicate])

This question already has an answer here:

(这个问题已经在这里有了答案:)

Can someone tell me how to detect if "specialword" appears in an array?

(有人可以告诉我如何检测"specialword"出现在数组中吗?)

Example:

(例:)

categories: [
    "specialword"
    "word1"
    "word2"
]
  ask by Cofey translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You really don't need jQuery for this.

(您真的不需要jQuery。)

var myarr = ["I", "like", "turtles"];
var arraycontainsturtles = (myarr.indexOf("turtles") > -1);

Hint : indexOf returns a number, representing the position where the specified searchvalue occurs for the first time, or -1 if it never occurs

(提示 :indexOf返回一个数字,表示指定的搜索值首次出现的位置;如果从未出现,则返回-1)

or

(要么)

function arrayContains(needle, arrhaystack)
{
    return (arrhaystack.indexOf(needle) > -1);
}

It's worth noting that array.indexOf(..) is not supported in IE < 9 , but jQuery's indexOf(...) function will work even for those older versions.

(值得注意的是IE <9不支持 array.indexOf(..) ,但jQuery的indexOf(...)函数即使在那些较旧的版本中也可以使用。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...