简体   繁体   English

JavaScript检查数组是否存在特定数字

[英]Javascript Checking array for presence of a specific number

I have search through quite a lot of questions here, but havent found one that i think fits my bill, so if you know of one please link to it. 我在这里搜索了很多问题,但是还没有找到我认为合适的问题,因此,如果您知道一个问题,请链接到它。

I have an array that i want to search through for a specific number and if that number is in the array, i then want to take an action and if not then another action. 我有一个数组,我想搜索一个特定的数字,如果该数字在数组中,那么我想采取一个操作,如果不是,则执行另一个操作。

I have something like this 我有这样的东西

var Array = ["1","8","17","14","11","20","2","6"];

for(x=0;x<=Array.length;x++)
{
    if(Array[x]==8)
        then change picture.src to srcpicture1
    else
        then change picture.src to srcpicture2
}

but this will run the lenght of the array and end up checking the last element of the array and since the last element is not 8 then it will change the picture to picture2. 但这将运行数组的长度,并最终检查数组的最后一个元素,由于最后一个元素不是8,则它将把图片更改为picture2。

Now i can see why this happens, i just dont have any ideas as to how to go about checking if an array contains a specific number. 现在,我明白了为什么会发生这种情况,我只是对如何检查数组是否包含特定数字一无所知。

Thanks in advance. 提前致谢。

What you can do is write yourself a function to check if an element belongs to an array: 您可以做的是为自己编写一个函数,以检查元素是否属于数组:

function inArray(array, value) {
    for (var i = 0; i < array.length; i++) {
        if (array[i] == value) return true;
    }
    return false;
}

And the just do: 和公正做:

var arr = ["1","8","17","14","11","20","2","6"];
if (inArray(arr, 8)) {
    // change picture.src to srcpicture1
} else {
    // change picture.src to srcpicture2
}

It's a lot more readable to me. 这对我来说更具可读性。


For extra points you can add the function to the array prototype like so: 对于其他方面,您可以将函数添加到数组原型中,如下所示:

Array.prototype.has = function (value) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] === value) return true;
    }
    return false;
};

And then the call would be 然后电话会是

if (arr.has(8)) // ...

Pushing this even further, you can check for indexOf() method on array and use it - if not - replace it with the code above. 进一步推动这一点,您可以检查数组上的indexOf()方法并使用它(如果没有),请用上面的代码替换它。


PS Try not to use Array for a variable name, since it's reserved for the actual array type. PS尽量不要将Array用作变量名,因为它是为实际的数组类型保留的。

Why don't just you abort the loop when you find the right number : 找到正确的数字后,为什么不终止循环:

for(x=0;x<=Array.length;x++)
{
    if(Array[x]==8) {
        //change picture.src to srcpicture1
        break;
    }
}

您可以先对数组进行排序,然后仅检查数组是否存在数组中存在的数字。

If you have unique keys and a faster retrieval is what you care about a lot, you can consider using a map instead of an array (if there's a hard-bound case of using an array, then it won't work of course). 如果您有唯一的键,而快速的检索是您最关心的事情,则可以考虑使用映射而不是数组(如果存在使用数组的硬约束情况,那当然是行不通的)。 If using a map, you just check "if( num in arr ) ". 如果使用地图,则只需选中“ if(num in arr)”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM