我有一个数组 tempArray = ["Kathmandu","Pokhara","Dharan"]
.为确保“Pokhara”在 tempArry 中,我必须使用循环并检查 tempArray 的每个元素。
有没有办法实现 Ruby 的 Array.include?
这样我就不需要使用循环了?
最佳答案
您可以使用 Array.indexOf
搜索值:
var includePokhara = ( tempArray.indexOf("Pokhara") >= 0 );
很遗憾,Array.indexOf
未在 Internet Explorer 中实现,但您可以在 StackOverflow 上查看 how to add it back .
关于javascript - 如何实现 Ruby 的 Array.include?在 JavaScript 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4203680/