Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找数组中是否存在javascript中的内容_Javascript_Arrays_Indexof - Fatal编程技术网

查找数组中是否存在javascript中的内容

查找数组中是否存在javascript中的内容,javascript,arrays,indexof,Javascript,Arrays,Indexof,我使用Javascript来查找数组中是否存在某些内容,但当我使用此代码时,它根本不会发出任何警报,这意味着它无法正常工作。我做错了什么 var theArray = new Array("one","two","three"); if (theArray.indexOf("one")) { alert("Found it!"); } if (!theArray.indexOf("four")) { alert("Did not find it!"); } 您应该将包含功能用作: if (t

我使用Javascript来查找数组中是否存在某些内容,但当我使用此代码时,它根本不会发出任何警报,这意味着它无法正常工作。我做错了什么

var theArray = new Array("one","two","three");

if (theArray.indexOf("one")) { alert("Found it!"); }
if (!theArray.indexOf("four")) { alert("Did not find it!"); }

您应该将
包含功能用作:

if (theArray.includes('one')) { alert("Found it!"); }

这是因为
indexOf
返回一个数字,该数字是匹配字符串的索引,如果该字符串在数组中不存在,则返回-1。您需要将返回值与以下数字进行比较:

if (thisArray.indexOf('one') > -1) { alert('Found it!') }
if (thisArray.indexOf('four') > -1) { alert('Did not find it') }
如果愿意,可以使用
includes
返回布尔值

if (thisArray.includes('one')) { alert('Found it!') }

请记住,数组的起始索引是
0

indexOf()方法返回给定元素所在的第一个索引 可以在数组中找到,如果不存在,则为-1

one
的索引为
0
,这是错误的,因此第一次检查将失败<如果不存在匹配项,code>indexOf
将返回
-1
,因此您应该明确检查该项

var theArray=新数组(“一”、“二”、“三”);
if(theArray.indexOf(“one”)!=-1{alert(“Found it!”);}
如果(array.indexOf(“four”)==-1{alert(“未找到它!”);}
您可以使用该运算符检查返回索引的值,如果未找到,则检查
-1

~
是一个。它非常适合与一起使用,因为如果找到索引
0,
indexOf
将返回。。。n
,如果没有-1:

value  ~value   boolean
-1  =>   0  =>  false
 0  =>  -1  =>  true
 1  =>  -2  =>  true
 2  =>  -3  =>  true
 and so on 
var theArray=新数组(“一”、“二”、“三”);
如果(~theArray.indexOf(“一”)){
log(“找到了!”);
}
如果(!~theArray.indexOf(“四”)){
log(“没有找到它!”);

}
你会认为这可能管用,不是吗?但这里有两个陷阱

  • 返回找到的第一个匹配元素的索引,如果没有找到任何内容,则返回
    -1
    。记住JavaScript数组的索引为零,这意味着第一个数组元素的索引为零。因此,如果匹配是第一个元素,那么零是返回值,并且像大多数语言一样,当您希望它为真时,零表示false。然而,这让我们想到了第二点

  • .indexOf()
    使用或换句话说
    =
    执行比较。返回值不会像使用
    true==1
    那样被强制。这在这里是非常相关的,因为如果它没有使用严格的等式,那么它找到的任何元素(第一个元素除外)的索引都是1或更高,那么您的比较就会成功。例如,
    if(array.indexOf(“two”)
    可以工作,因为该元素的索引是
    1
    。但是,如果single
    indexOf()
    执行严格的相等性检查,则会失败。这就是为什么您需要显式地将
    indexOf()
    的返回值与某个值进行比较,通常是
    -1


  • 线性搜索。这是一种“语言不可知”的方法,用于解决搜索无序列表的问题。是的,您可以使用
    array.includes()
    ,这是JavaScript特有的简洁的。但是,您似乎对编程还不熟悉,至少对JavaScript是这样,在您利用那些使生活更轻松的奇特工具之前,您应该亲自实现它们,这样您才能真正了解在幕后发生了什么,更重要的是,为什么它会起作用

    function contains(array, value) {
    
    
        // Loop through the entire array
        for (let i = 0; i < array.length; i++) {
    
            // Return true on first match
            if (array[i] === value)
                return true
        }
    
        // Return false on no match
        return false
    }
    
    // Make an array
    let a = ['one', 'two', 'three']
    
    // If it has an element, notify
    if (contains(a, 'one'))
        alert('found it')
    
    函数包含(数组、值){
    //循环遍历整个阵列
    for(设i=0;i
    indexOf()
    在找不到项时返回-1(不是false)这在IE:plus 1中不起作用,我的意思是+1或者简单地使用+1(作为类似类型的ans)
    if(thisrarray.indexOf('four')>-1)
    应该是
    if(thisrarray.indexOf('four'))