JavaScript等同于PHP的in_array()

JavaScript中是否有一种方法可以比较一个数组中的值并查看它是否在另一个数组中?


类似于PHP的in_array功能?


小唯快跑啊
浏览 412回答 3
3回答

大话西游666

不,它没有一个。因此,大多数流行的库在其实用程序包中都附带一个库。查看jQuery的inArray和Prototype的Array.indexOf的示例。jQuery的实现就像您期望的那样简单:function inArray(needle, haystack) {&nbsp; &nbsp; var length = haystack.length;&nbsp; &nbsp; for(var i = 0; i < length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if(haystack[i] == needle) return true;&nbsp; &nbsp; }&nbsp; &nbsp; return false;}如果您要处理大量的数组元素,那么上面的技巧就可以很好地解决问题。编辑:哎呀。我什至没有注意到您想查看一个数组是否在另一个数组中。根据PHP文档,这是PHP的预期行为in_array:$a = array(array('p', 'h'), array('p', 'r'), 'o');if (in_array(array('p', 'h'), $a)) {&nbsp; &nbsp; echo "'ph' was found\n";}if (in_array(array('f', 'i'), $a)) {&nbsp; &nbsp; echo "'fi' was found\n";}if (in_array('o', $a)) {&nbsp; &nbsp; echo "'o' was found\n";}// Output://&nbsp; 'ph' was found//&nbsp; 'o' was found克里斯和亚历克斯发布的代码不遵循此行为。Alex是Prototype的indexOf的正式版本,而Chris的更像是PHP的array_intersect。这就是您想要的:function arrayCompare(a1, a2) {&nbsp; &nbsp; if (a1.length != a2.length) return false;&nbsp; &nbsp; var length = a2.length;&nbsp; &nbsp; for (var i = 0; i < length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (a1[i] !== a2[i]) return false;&nbsp; &nbsp; }&nbsp; &nbsp; return true;}function inArray(needle, haystack) {&nbsp; &nbsp; var length = haystack.length;&nbsp; &nbsp; for(var i = 0; i < length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if(typeof haystack[i] == 'object') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(arrayCompare(haystack[i], needle)) return true;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(haystack[i] == needle) return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return false;}这是我对上面的测试:var a = [['p','h'],['p','r'],'o'];if(inArray(['p','h'], a)) {&nbsp; &nbsp; alert('ph was found');}if(inArray(['f','i'], a)) {&nbsp; &nbsp; alert('fi was found');}if(inArray('o', a)) {&nbsp; &nbsp; alert('o was found');}&nbsp;&nbsp;// Results://&nbsp; &nbsp;alerts 'ph' was found//&nbsp; &nbsp;alerts 'o' was found请注意,我故意不扩展Array原型,因为这样做通常是一个坏主意。

有只小跳蛙

Array.indexOf是在JavaScript 1.6中引入的,但较旧的浏览器不支持。幸运的是,Mozilla的麻烦为您完成了所有艰苦的工作,并为您提供了兼容性:if (!Array.prototype.indexOf){&nbsp; Array.prototype.indexOf = function(elt /*, from*/)&nbsp; {&nbsp; &nbsp; var len = this.length >>> 0;&nbsp; &nbsp; var from = Number(arguments[1]) || 0;&nbsp; &nbsp; from = (from < 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;? Math.ceil(from)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Math.floor(from);&nbsp; &nbsp; if (from < 0)&nbsp; &nbsp; &nbsp; from += len;&nbsp; &nbsp; for (; from < len; from++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; if (from in this &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this[from] === elt)&nbsp; &nbsp; &nbsp; &nbsp; return from;&nbsp; &nbsp; }&nbsp; &nbsp; return -1;&nbsp; };}甚至还有一些方便的用法摘要,可助您编写脚本。

扬帆大鱼

如果索引不是按顺序排列的,或者索引不是连续的,则此处列出的其他解决方案中的代码将中断。一个更好用的解决方案可能是:function in_array(needle, haystack) {&nbsp; &nbsp; for(var i in haystack) {&nbsp; &nbsp; &nbsp; &nbsp; if(haystack[i] == needle) return true;&nbsp; &nbsp; }&nbsp; &nbsp; return false;}而且,作为奖励,这等效于PHP的array_search(用于查找数组中元素的键:function array_search(needle, haystack) {&nbsp; &nbsp; for(var i in haystack) {&nbsp; &nbsp; &nbsp; &nbsp; if(haystack[i] == needle) return i;&nbsp; &nbsp; }&nbsp; &nbsp; return false;}
打开App,查看更多内容
随时随地看视频慕课网APP