From: Mike on
I have a list of numbers, e.g., (1,3,4,5,8,16,20), and am trying to
create a simple IF statement to see if the value is in that list. Is
there an easier or more efficient way, than the sample code below, to
do it?

=====

<script type="text/javascript">
num = 2;
list = [1,3,4,5,8,16,20];
if(isInList( num, list )) {
alert("It's there!");
} else {
alert("It's NOT there!");
}

function isInList( num, list ) {
// List is an Array()
result = false;
for(i in list) {
if(num == list[i]) { result = true }
}
return result
}
</script>
From: Tom Cole on
On Apr 22, 2:43 pm, Mike <mpea...(a)gmail.com> wrote:
> I have a list of numbers, e.g., (1,3,4,5,8,16,20), and am trying to
> create a simple IF statement to see if the value is in that list. Is
> there an easier or more efficient way, than the sample code below, to
> do it?
>
> =====
>
> <script type="text/javascript">
> num = 2;
> list = [1,3,4,5,8,16,20];
> if(isInList( num, list )) {
> alert("It's there!");} else {
>
> alert("It's NOT there!");
>
> }
>
> function isInList( num, list ) {
> // List is an Array()
> result = false;
> for(i in list) {
> if(num == list[i]) { result = true }
> }
> return result}
>
> </script>

What about alert(num in list);
From: Mike on
> What about alert(num in list);- Hide quoted text -

Yes, that would work, but I'm actually concerned about the isInList
function. I imagine there's some way to avoid the whole for(i in
list) routine. For example, is there a way to do this:

function isInList( number, list ) {
if(number == anyElementIn( list ) { return true } else { return
false }
}

is there some built in javascript method that will check a value
against any value in an Array?

Thanks!

Mike
From: Tom Cole on
On Apr 22, 3:12 pm, Mike <mpea...(a)gmail.com> wrote:
> > What about alert(num in list);- Hide quoted text -
>
> Yes, that would work, but I'm actually concerned about the isInList
> function. I imagine there's some way to avoid the whole for(i in
> list) routine. For example, is there a way to do this:
>
> function isInList( number, list ) {
> if(number == anyElementIn( list ) { return true } else { return
> false }
>
> }
>
> is there some built in javascript method that will check a value
> against any value in an Array?
>
> Thanks!
>
> Mike

I've looked around as I'm not aware of one. The best I can come up
with is:

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

This method returns immediately upon a success, rather than looping
through the entire array every time as your first example did.
From: Peroli on
On Apr 23, 12:12 am, Mike <mpea...(a)gmail.com> wrote:
> > What about alert(num in list);- Hide quoted text -
>
> Yes, that would work, but I'm actually concerned about the isInList
> function. I imagine there's some way to avoid the whole for(i in
> list) routine. For example, is there a way to do this:
>
> function isInList( number, list ) {
> if(number == anyElementIn( list ) { return true } else { return
> false }
>
> }
>
> is there some built in javascript method that will check a value
> against any value in an Array?
>
> Thanks!
>
> Mike

Mike,
You can use "indexOf" (Javascript 1.6, so won't work in IE) method
on an array instead. But its performance differs from browser to
browser.
"num in list" is faster in Firefox, but slower in Safari.


--- script ---

<html>
<head>
<title>Benchmark</title>
</head>
<body>
<form id="test_form">
<input type="button" value="Benchmark" onclick="benchmark()">
</form>
<span id="result" />
<script type="text/javascript">
function benchmark() {
var a = [1,3,4,5,8,16,20];
var d = new Date();
for(var i=0; i<100000; i++) {
var a1 = a.indexOf(8);
}
var t1 = (new Date()) - d;
d = new Date();
for(var i=0; i<100000; i++) {
var a1 = 8 in a;
}
var t2 = (new Date()) - d;
document.getElementById('result').innerHTML = "indexOf:" + t1 +
'<br />in:' + t2;
}
</script>
</body>
</html>

-- Peroli Sivaprakasam