JavaScript 相当于 PHP 的 in_array()
JavaScript 有没有一种方法可以比较一个数组中的值并查看它是否在另一个数组中?
类似于 PHP 的 in_array
函数?
Is there a way in JavaScript to compare values from one array and see if it is in another array?
Similar to PHP's in_array
function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
不,它没有。 因此,大多数流行的库都在其实用程序包中附带了一个库。 查看 jQuery 的 inArray 和 Prototype 的Array.indexOf。
jQuery 的实现就像您所期望的一样简单:
如果您正在处理数量合理的数组元素,上面的方法就可以很好地实现这一点。
编辑:哎呀。 我什至没有注意到你想查看一个数组是否在另一个数组内。 根据 PHP 文档,这是 PHP
in_array
的预期行为:代码Chris 和 Alex 发布的内容不遵循此行为。 Alex的是Prototype的indexOf的官方版本,Chris的更像是PHP的
array_intersect
。 这就是你想要的:这是我对上面的测试:
请注意,我故意没有扩展数组原型,因为这样做通常是一个坏主意。
No, it doesn't have one. For this reason most popular libraries come with one in their utility packages. Check out jQuery's inArray and Prototype's Array.indexOf for examples.
jQuery's implementation of it is as simple as you might expect:
If you are dealing with a sane amount of array elements the above will do the trick nicely.
EDIT: Whoops. I didn't even notice you wanted to see if an array was inside another. According to the PHP documentation this is the expected behavior of PHP's
in_array
:The code posted by Chris and Alex does not follow this behavior. Alex's is the official version of Prototype's indexOf, and Chris's is more like PHP's
array_intersect
. This does what you want:And this my test of the above on it:
Note that I intentionally did not extend the Array prototype as it is generally a bad idea to do so.
现在有
Array.prototype.includes
:
There is now
Array.prototype.includes
:Array.indexOf
引入于JavaScript 1.6,但旧版浏览器不支持。 值得庆幸的是,Mozilla 的小伙子们已经完成了所有艰苦的工作 为您提供,并为您提供了此兼容性:甚至还有一些方便的使用片段,供您享受脚本编写的乐趣。
Array.indexOf
was introduced in JavaScript 1.6, but it is not supported in older browsers. Thankfully the chaps over at Mozilla have done all the hard work for you, and provided you with this for compatibility:There are even some handy usage snippets for your scripting pleasure.
PHP方式:
我的JS解决方案:
PHP way:
My solution in JS:
您可以简单地使用“includes”函数,如 w3schools 课程中所述,
它看起来像
You can simply use the "includes" function as explained in this lesson on w3schools
it looks like
如果索引不按顺序,或者索引不连续,则此处列出的其他解决方案中的代码将会损坏。 一个更好的解决方案可能是:
而且,作为奖励,这里相当于 PHP 的 array_search (用于查找数组中元素的键:
If the indexes are not in sequence, or if the indexes are not consecutive, the code in the other solutions listed here will break. A solution that would work somewhat better might be:
And, as a bonus, here's the equivalent to PHP's array_search (for finding the key of the element in the array:
有一个名为 Locutus 的项目,它在 Javascript 中实现了 PHP 函数和 in_array() 已包含在内,您可以像在 PHP 中一样使用它。
使用示例:
There is a project called Locutus, it implements PHP functions in Javascript and in_array() is included, you can use it exactly as you use in PHP.
Examples of use:
jQuery 解决方案可用,请在此处查看文档:
http://api.jquery.com/jquery.inarray/
jQuery solution is available, check the ducumentation here:
http://api.jquery.com/jquery.inarray/
有一个等效的函数:
看这里:
https://developer.mozilla.org/ en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
There is an equivalent function:
Look here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
如果您只想检查数组中是否有单个值,那么 Paolo 的代码就可以完成这项工作。 如果您想检查两个数组共有哪些值,那么您需要这样的东西(使用 Paolo 的 inArray 函数):
这将返回一个包含
a
和中的值的数组>b
。 (从数学上讲,这是两个数组的交集。)编辑:请参阅 Paolo 编辑的代码,了解您的问题的解决方案问题。 :)
If you only want to check if a single value is in an array, then Paolo's code will do the job. If you want to check which values are common to both arrays, then you'll want something like this (using Paolo's inArray function):
This wil return an array of values that are in both
a
andb
. (Mathematically, this is an intersection of the two arrays.)EDIT: See Paolo's Edited Code for the solution to your problem. :)
如果您需要所有 PHP 可用参数,请使用以下命令:
If you need all the PHP available parameters, use this:
将此代码添加到您的项目中并使用对象样式的 inArray 方法
Add this code to you project and use the object-style inArray methods
对于 Dojo Toolkit,您可以使用
dojo.indexOf()
。 请参阅 dojo.indexOf 了解文档,以及< em>Arrays Made Easy,作者:Bryan Forbes,提供了一些示例。With Dojo Toolkit, you would use
dojo.indexOf()
. See dojo.indexOf for the documentation, and Arrays Made Easy by Bryan Forbes for some examples.其中 haystack 是一个数组,needle 是数组中的一个元素。 如果未找到元素,则返回未定义的元素,否则返回相同的元素。
where haystack is an array and needle is an element in array. If element not found will be returned undefined else the same element.
我找到了一个很棒的 jQuery 解决方案 这里关于SO。
我希望有人能发布一个如何在下划线中执行此操作的示例。
I found a great jQuery solution here on SO.
I wish someone would post an example of how to do this in underscore.
带有
underscore
的in_array
的等效项是 _.indexOf示例:
_.indexOf([3, 5, 8], 8); // 返回 2,索引 8
_.indexOf([3, 5, 8], 10); // 返回-1,未找到
An equivalent of
in_array
withunderscore
is _.indexOfExamples:
_.indexOf([3, 5, 8], 8); // returns 2, the index of 8
_.indexOf([3, 5, 8], 10); // returns -1, not found
如果您打算在课堂上使用它,并且希望它具有功能性(并且可以在所有浏览器中工作):
希望它对某人有帮助:-)
If you are going to use it in a class, and if you prefer it to be functional (and work in all browsers):
Hope it helps someone :-)