javascript jquery 检查数字是否在列表中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17931288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:08:55  来源:igfitidea点击:

jquery check if number is in a list

javascriptjqueryarrays

提问by Anders

I have to check whether a variable is equal to a given number or another. For example I am doing this right now.

我必须检查一个变量是否等于给定的数字或另一个。例如,我现在正在这样做。

if (num == 1 || num == 3 || num == 4 || etc.) {
    // Do something
} else if (num == 2 || num == 7 || num == 11 || etc.) {
    // Do something
}

I thought there should be an easier way. for example an array of all numbers per if statement.

我认为应该有一种更简单的方法。例如,每个 if 语句的所有数字的数组。

var array1 = [1,3,4,5,6,8,9,10 etc.]
var array2 = [2,7,11,12,13,14 etc.]

And then see if the number is equal to anything inside one of these arrays. But I don't know how to do it..

然后查看该数字是否等于这些数组之一中的任何内容。但我不知道该怎么做..

回答by David

The indexOf() method searches the array for the specified item, and returns its position.

indexOf() 方法在数组中搜索指定项,并返回其位置。

 var array1 = [1,3,4,5,6,8,9,10];
 var a = array1.indexOf(46); //a = -1; if not found

If you need to support environments that don't have .indexOf(), you could implement the MDN fix.

如果您需要支持没有 .indexOf() 的环境,您可以实施MDN 修复。

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
        "use strict";

        if (this === void 0 || this === null) throw new TypeError();

        var t = Object(this);
        var len = t.length >>> 0;
        if (len === 0) return -1;

        var n = 0;
        if (arguments.length > 0) {
            n = Number(arguments[1]);
            if (n !== n) // shortcut for verifying if it's NaN
            n = 0;
            else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) n = (n > 0 || -1) * Math.floor(Math.abs(n));
        }

        if (n >= len) return -1;

        var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);

        for (; k < len; k++) {
            if (k in t && t[k] === searchElement) return k;
        }
        return -1;
    };
}

回答by jdepypere

Since you're asking for jQuery, there is .inArray(). This returns a -1if it isn't found, else the index of the matching element.

由于您要求使用 jQuery,因此有.inArray()-1如果未找到,则返回 a ,否则返回匹配元素的索引。

回答by Jivings

You don't need to use jQuery. This is a built in function called indexOf:

您不需要使用 jQuery。这是一个内置函数,称为indexOf

if ( arr.indexOf(item) !== -1 ) {
    // item is in array
}

回答by Armen

why dont you do a simple for loop?

你为什么不做一个简单的 for 循环?

for(var i = 0; i < array1.length; i++)
{
  if(array[i] == num)
   {
     //     do something
     break;
   }
}

回答by simongus

You might use inArrayfunction (http://api.jquery.com/jQuery.inArray/)

您可能会使用inArray函数(http://api.jquery.com/jQuery.inArray/

<script>var arr = [ 4, "Pete", 8, "John" ];
var $spans = $("span");
$spans.eq(0).text(jQuery.inArray("John", arr));
$spans.eq(1).text(jQuery.inArray(4, arr));
$spans.eq(2).text(jQuery.inArray("Karl", arr));
$spans.eq(3).text(jQuery.inArray("Pete", arr, 2));
</script>

Output:

输出:

"John" found at 3 4 found at 0 "Karl" not found, so -1 "Pete" is in the array, but not at or after index 2, so -1

"John" found at 3 4 found at 0 "Karl" not found, 所以 -1 "Pete" 在数组中,但不在索引 2 处或之后,所以 -1

回答by Mihail

You can do it without jQuery:

你可以在没有 jQuery 的情况下做到:

if(array1.indexOf(num1) >= 0)//if -1, then not found