我现在用来检查这个功能如下:
function inArray(needle,haystack)
{
var count=haystack.length;
for(var i=0;i<count;i++)
{
if(haystack[i]===needle){return true;}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
有用.我正在寻找的是,是否有更好的方法来做到这一点.
至于详细的其他地方,否则显然是众所周知的,IE浏览器(版本绝对7,并在某些情况下,版本8)不落实的关键功能,特别是Array(如forEach,indexOf等).
这里和那里有许多变通方法,但我想将一组适当的,规范的实现折叠到我们的网站中,而不是复制,粘贴或破解我们自己的实现.我找到了js-methods,看起来很有前景,但我想在这里发帖看看是否有更高度推荐的另一个库.一些杂项标准:
除了可读性之外,使用之间是否存在任何可辨别的差异(可能性能)
str.indexOf("src")
Run Code Online (Sandbox Code Playgroud)
和
str.match(/src/)
Run Code Online (Sandbox Code Playgroud)
我个人更喜欢match(和regexp),但同事似乎走了另一条路.我们想知道它是否重要......?
编辑:
我应该在一开始就说过,这是用于执行部分普通字符串匹配的函数(在JQuery的类属性中获取标识符),而不是使用通配符等进行完整的正则表达式搜索.
class='redBorder DisablesGuiClass-2345-2d73-83hf-8293'
Run Code Online (Sandbox Code Playgroud)
所以它的区别在于:
string.indexOf('DisablesGuiClass-');
Run Code Online (Sandbox Code Playgroud)
VS
string.match(/DisablesGuiClass-/)
Run Code Online (Sandbox Code Playgroud) 什么是将一个值与多个选项进行比较的最漂亮的方法?
我知道有很多方法可以做到这一点,但我正在寻找最好的方法.
我问,因为我希望这是可行的(当你看它时,它显然不是这样):
if (foobar == (foo||bar) ) {
//do something
}
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
var ar = [10,7,8,3,4,7,6];
function isin(n,a){
for (var i=0;i<a.length;i++){
if (a[i]== n) {
var b = true;
return b;
} else {
var c = false;
return c;
}
}
}
function unique(a){
var arr = [];
for (var i=0;i<a.length;i++){
if (!isin(a[i],arr)){
arr.push(a[i]);
}
}
return arr;
}
alert(unique(ar));
Run Code Online (Sandbox Code Playgroud)
在这段代码中,我尝试从原始数组创建新的唯一数组(没有重复).但我仍然得到原始阵列!哪里是我的错?
我正在阅读这篇博文,上面提到了:
!!~
Run Code Online (Sandbox Code Playgroud)
我不知道这是做什么的?起初我以为它会给出错误,但下面的代码确实运行:
var _sessions = [
"_SID_1",
"_SID_2",
"_SID_3",
"_SID_4"
];
if(!!~_sessions.indexOf("_SID_5")) {
console.log('found');
} else {
console.log('!found');
}
Run Code Online (Sandbox Code Playgroud)
输出:
node test.js
!found
Run Code Online (Sandbox Code Playgroud) 我需要在jQuery cookie中存储一个数组,任何人都可以帮助我吗?
我的问题是即使对于重复的条形码,循环也会继续进入if语句.我正在尝试仅为唯一条形码输入if语句,但在循环结束时myArray中有重复项....为什么?
var myArray = new Array(); var i = 0;
$("li.foo").each(function(){
var iBarCode = $(this).attr('barcode');
if( !( iBarCode in myArray ) ){
myArray[i++] = iBarCode;
//do something else
}
});
Run Code Online (Sandbox Code Playgroud) 假设我有一个数组= [0,8,5]
知道8是否在这个内部的最快方法是什么...例如:
if(array.contain(8)){
// return true
}
Run Code Online (Sandbox Code Playgroud)
我发现了这个:检查列表中是否存在值的最快方法(Python)
而这:检测Javascript中的值是否在一组值中的最快方法
但这不能回答我的问题.谢谢.
在Firefox和Opera上的Javascript中使用indexOf调用获取错误.在IE中正常工作.
以下是错误消息:
行动
function anonymous(Grid, Row, Col, Event) {
return Grid.ActionShowPopupMenu();
}
Run Code Online (Sandbox Code Playgroud)
for事件OnRightClick失败,异常:row.id.indexOf不是函数
我正在测试一个字符串在Javascript中包含另一个字符串并使用字符串的indexOf函数.然而,调用是在JQuery函数中进行的.也许这就是问题的原因?是否有替代方法在Javascript中使用indexOf来测试字符串是否包含另一个字符串?这个问题有解决方法吗?
javascript ×9
arrays ×5
jquery ×3
string ×2
client ×1
comparison ×1
cookies ×1
each ×1
indexof ×1
performance ×1
regex ×1
testing ×1