【问题标题】:Find out if a variable is in an array? [duplicate]找出变量是否在数组中? [复制]
【发布时间】:2012-11-10 16:08:40
【问题描述】:

我有一个变量:

var code = "de";

我有一个数组:

var countryList = ["de","fr","it","es"];

有人可以帮助我,因为我需要检查变量是否在 countryList 数组中 - 我的尝试在这里:

    if (code instanceof countryList) {
        alert('value is Array!');
    } 

    else {
        alert('Not an array');
    }

但是运行时我在 console.log 中收到以下错误:

TypeError: 'instanceof' 操作数国家列表无效

【问题讨论】:

  • -1 tag question with jquery if you want to get solution of jquery ...它具有误导性,在此答案之前给出第二个相同的答案 y 未标记为 asnwer ...我想知道会发生什么如果在接受答案之前给出的相同答案没有被接受...

标签: javascript arrays


【解决方案1】:

你需要使用Array.indexOf:

if (countryList.indexOf(code) >= 0) {
   // do stuff here
}

请注意它在 IE8 和之前的版本(可能还有其他旧版浏览器)中不受支持。了解更多信息here

【讨论】:

【解决方案2】:

jQuery 有一个实用函数来判断一个元素是否存在于数组中

$.inArray(value, array)

如果数组中不存在值,则返回 array-1 中值的索引。所以你的代码可以是这样的

if( $.inArray(code, countryList) != -1){
     alert('value is Array!');
} else {
    alert('Not an array');
}

【讨论】:

    【解决方案3】:

    您似乎在寻找Array.indexOf 函数。

    【讨论】:

      【解决方案4】:

      instanceof 用于检查对象是否属于某种类型(这是一个完全不同的主题)。因此,您应该在数组中查找,而不是您编写的代码。您可以像这样检查每个元素:

      var found = false;
      for( var i = 0; i < countryList.length; i++ ) {
        if ( countryList[i] === code ) {
          found = true;
          break;
        }
      }
      
      if ( found ) {
        //the country code is not in the array
        ...
      } else {
        //the country code exists in the array
        ...
      }
      

      或者你可以使用indexOf()函数的更简单的方法。每个数组都有一个indexOf() 函数,它循环一个元素并返回它在数组中的索引。如果找不到元素,则返回-1。因此,您检查indexOf() 的输出,看看它是否在数组中找到与您的字符串匹配的任何内容:

      if (countryList.indexOf(code) === -1) {
        //the country code is not in the array
        ...
      } else {
        //the country code exists in the array
        ...
      }
      

      我会使用第二种算法,因为它更简单。但是第一个算法也很好,因为它更具可读性。两者的收入相同,但第二个性能更好,时间更短。但是,旧浏览器(IE

      如果您使用的是 JQuery 库,您可以使用适用于所有浏览器的 inArray() 函数。它与indexOf() 相同,如果找不到您要查找的元素,则返回-1。所以你可以这样使用它:

      if ( $.inArray( code, countryList ) === -1) {
        //the country code is not in the array
        ...
      } else {
        //the country code exists in the array
        ...
      }
      

      【讨论】:

        【解决方案5】:

        在jquery中你可以使用

        jQuery.inArray()-在数组中搜索指定值并返回其索引(如果未找到则返回-1)。

        if ($.inArray('de', countryList ) !== -1) 
        {
        }
        

        对于 javascript 解决方案检查现有的How do I check if an array includes an object in JavaScript?

        Array.prototype.contains = function(k) {
            for(p in this)
                if(this[p] === k)
                    return true;
            return false;
        }
        for example:
        
        var list = ["one","two"];
        
        list.contains("one") // returns true
        

        【讨论】:

        • 简单问题 y 有 -1 吗?请对此发表评论
        • 我认为投反对票的人(不是我)这样做是因为它最初只用 JavaScript 标记并且无法假设他正在使用库。但是现在很明显他想要 JQuery,所以 +1 作为正确答案。
        • @user1394965 - 问题是 -1 在 OP 选择答案后发生
        【解决方案6】:

        对于纯 JavaScript 解决方案,您可以只遍历数组。

        function contains( r, val ) {
            var i = 0, len = r.length;
        
            for(; i < len; i++ ) {
                if( r[i] === val ) {
                    return i;
                }
             }
             return -1;
        }
        

        【讨论】:

          【解决方案7】:

          使用 jQuery

          您可以使用$.inArray()

          $.inArray(value, array)
          

          返回项目的索引,如果没有找到则返回 -1

          【讨论】:

          • 我认为这是 jQuery,而不是核心 Javascript。
          • 是的,这是jquery,使用jquery有什么问题!
          • 他没有提到他不想要 JQuery。我认为inArray() 是一种很好的技术,因为indexOf() 在订单浏览器中不可用。 +1
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-12-27
          • 2011-08-07
          • 2021-11-20
          • 1970-01-01
          • 2013-04-03
          • 1970-01-01
          相关资源
          最近更新 更多