rof*_*fle 201 javascript php phpjs
有没有办法在JavaScript中比较一个数组的值,看看它是否在另一个数组中?
类似于PHP的in_array功能?
Pao*_*ino 245
不,它没有.出于这个原因,大多数流行的库都带有一个实用程序包.查看jQuery的inArray和Prototype的Array.indexOf作为示例.
jQuery的实现就像你期望的那样简单:
function inArray(needle, haystack) {
var length = haystack.length;
for(var i = 0; i < length; i++) {
if(haystack[i] == needle) return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
如果你正在处理一个理智的数组元素,上面将很好地做到这一点.
编辑:哎呀.我甚至没有注意到你想看看阵列是否在另一个阵列中.根据PHP文档,这是PHP的预期行为in_array:
$a = array(array('p', 'h'), array('p', 'r'), 'o');
if (in_array(array('p', 'h'), $a)) {
echo "'ph' was found\n";
}
if (in_array(array('f', 'i'), $a)) {
echo "'fi' was found\n";
}
if (in_array('o', $a)) {
echo "'o' was found\n";
}
// Output:
// 'ph' was found
// 'o' was found
Run Code Online (Sandbox Code Playgroud)
Chris和Alex发布的代码不遵循此行为.Alex是Prototype的indexOf的官方版本,而Chris的更像是PHP的版本array_intersect.这样做你想要的:
function arrayCompare(a1, a2) {
if (a1.length != a2.length) return false;
var length = a2.length;
for (var i = 0; i < length; i++) {
if (a1[i] !== a2[i]) return false;
}
return true;
}
function inArray(needle, haystack) {
var length = haystack.length;
for(var i = 0; i < length; i++) {
if(typeof haystack[i] == 'object') {
if(arrayCompare(haystack[i], needle)) return true;
} else {
if(haystack[i] == needle) return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
这是我对上面的测试:
var a = [['p','h'],['p','r'],'o'];
if(inArray(['p','h'], a)) {
alert('ph was found');
}
if(inArray(['f','i'], a)) {
alert('fi was found');
}
if(inArray('o', a)) {
alert('o was found');
}
// Results:
// alerts 'ph' was found
// alerts 'o' was found
Run Code Online (Sandbox Code Playgroud)
请注意,我故意没有扩展Array原型,因为这通常是一个坏主意.
Ale*_*ett 65
Array.indexOf是在JavaScript 1.6中引入的,但旧版浏览器不支持它.值得庆幸的是,Mozilla的所有工作都为您完成了所有艰苦的工作,并为您提供兼容性:
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}
Run Code Online (Sandbox Code Playgroud)
甚至还有一些方便的使用片段为您的脚本乐趣.
ale*_*rus 49
includes()方法确定数组是否包含某个元素,并根据需要返回true或false.
var a = [1, 2, 3];
a.includes(2); // true
a.includes(4); // false
Run Code Online (Sandbox Code Playgroud)
句法
arr.includes(searchElement)
arr.includes(searchElement, fromIndex)
Run Code Online (Sandbox Code Playgroud)
fse*_*enm 19
PHP方式:
if (in_array('a', ['a', 'b', 'c'])) {
// do something if true
}
Run Code Online (Sandbox Code Playgroud)
我在 JS 中的解决方案:
if (['a', 'b', 'c'].includes('a')) {
// do something if true
}
Run Code Online (Sandbox Code Playgroud)
ran*_*ame 14
如果索引不是按顺序排列,或者索引不是连续的,则此处列出的其他解决方案中的代码将会中断.一个可以更好地工作的解决方案可能是:
function in_array(needle, haystack) {
for(var i in haystack) {
if(haystack[i] == needle) return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
而且,作为奖励,这里相当于PHP的array_search(用于查找数组中元素的键:
function array_search(needle, haystack) {
for(var i in haystack) {
if(haystack[i] == needle) return i;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
小智 11
您可以简单地使用w3schools 上本课中所述的“包含”功能
看起来像
let myArray = ['Kevin', 'Bob', 'Stuart'];
if( myArray.includes('Kevin'))
console.log('Kevin is here');Run Code Online (Sandbox Code Playgroud)
有一个名为Locutus的项目,它在Javascript中实现了PHP函数,并且包含了in_array(),你可以像在PHP中一样使用它.
使用示例:
in_array('van', myArray);
in_array(1, otherArray, true); // Forcing strict type
Run Code Online (Sandbox Code Playgroud)
var a = [1,2,3,4,5,6,7,8,9];
var isSixInArray = a.filter(function(item){return item==6}).length ? true : false;
var isSixInArray = a.indexOf(6)>=0;
Run Code Online (Sandbox Code Playgroud)
可以使用jQuery解决方案,请在此处检查文档:http ://api.jquery.com/jquery.inarray/
$.inArray( 10, [ 8, 9, 10, 11 ] );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
228031 次 |
| 最近记录: |