最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Checking array for presence of a specific number - Stack Overflow

programmeradmin7浏览0评论

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.

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.

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.

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.

Share Improve this question asked Nov 25, 2009 at 9:40 Thorbjørn Reimann-AndersenThorbjørn Reimann-Andersen 1033 silver badges13 bronze badges 2
  • I suggest you don't use Array as a variable name. Even if it works (of which I'm not sure), it might cause problems. – Joel Commented Nov 25, 2009 at 9:50
  • i know, i dont actually, i forgot to write myArray – Thorbjørn Reimann-Andersen Commented Nov 25, 2009 at 9:54
Add a ment  | 

5 Answers 5

Reset to default 7

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.


P.S. Try not to use Array for a variable name, since it's reserved for the actual array type.

use this

http://developer.mozilla/En/Core_JavaScript_1.5_Reference/Objects/Array/IndexOf

ie version

https://developer.mozilla/En/Core_JavaScript_1.5_Reference/Objects/Array/IndexOf#Compatibility

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;
    }
}

You could sort the array first then check the array only up to the point at which a number would be in the array, were it to exist.

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 ) ".

发布评论

评论列表(0)

  1. 暂无评论