PDA

View Full Version : Javascript equivalent to list.contains("String")


b.mic
04-15-2004, 04:24 AM
Hello,

I have a series of words (server-side generated) that counts about 300 items (but likely to grow in time), and I would like to check (client side) whether what the user typed in a certain field matches one of the words in the series.

To do this, I know I can put that series of words in a javascript array, then iterate through the array and test each word for equivalence to the user input.

Or maybe put all the words in one long string and then use regexp.

As for the first option, I'm afraid of the time cost if the series of words gets bigger.

As for the second option, as I don't know the content of the series of words and the user input, I think it could be a bit tricky.

I was wondering if there was a javascript equivalent to the Java "List" object (or, more precisely, "ArrayList", as "List" is an interface), which has a method "contains()" that exactly fits my need. I didn't see anything like that in the javascript doc but ...

Or if anyone has a good idea about how to achieve this, it would be most welcome.

Thank you,

[B]

Willy Duitt
04-15-2004, 05:14 AM
You do not say what you intend to do if you find a match. :eek:
But, below is a script I wrote Goldie a little while ago which iterates thru an array of words and checks if those words were entered into the text box. In this example, if the word matches, the word is converted to upperCase.

<html>
<head>
<script type="text/javascript">
<!--//
function capAcrons(){
var acron = new Array('ape','boy','cat','dog','elf','fog');
var a = document.f.t1.value.split(/\s+/g);
for(var count=0; count<acron.length; count++){
var myregex = new RegExp('^'+acron[count]+'\$', 'i');
for(var i=0; i<a.length; i++){
if(a[i].match(myregex)){
c=a[i].toUpperCase();
a[i]=c;
} a[i]=a[i];
} document.f.t1.value = a.join(' ');
}
}
// -->
</script>
</head>
<body>
<form name="f">
<input type="text" name="t1" size="50"
value=" ape Cat dog cats apes cat fog fogs elves elf ">
<br>
<input type="button" value="Acronyms toUpperCase" onClick="capAcrons()">
</form>
</body>
</html>

.....Willy

b.mic
04-15-2004, 05:35 AM
Thank you for your answer.

You do not say what you intend to do if you find a match.

Just warn the user (with an alert box).

iterates thru an array of words and checks if those words were entered into the text box.

Yes, that's what I'm doing right now. But as I said, I'm afraid of performance as the list grows bigger. It's working fine now with about 300 hundreds items, but ...

Thus I was wondering if there was any way to do this quicker.

Anyway, thank you.

[B]

Willy Duitt
04-15-2004, 05:50 AM
Originally posted by b.mic
Yes, that's what I'm doing right now.

If you already have a code which iterates thru the array. Post your codes and perhaps someone could spot an easier/faster or more efficient way of doing it.

.....Willy

Vincent Puglia
04-15-2004, 08:28 AM
An associative array should be faster:


<script type="text/javascript" language="javascript">
var theList = new Array()
theList['cat'];
theList['dog'];
theList['fish'];
function has(theObj)
{
txt = theObj.value
if (theList[txt]) alert(txt)
}
</script>
</head>
<body>
<form name="theForm">
<input type="text" onblur='has(this)'>
</form>
</body>


Note: I tried calling the function 'contains' and got errors (possibly a reserved word)

Vinny

b.mic
04-16-2004, 05:50 AM
Hello,

Thank you for your suggestion.

But to have it working, you need to assign a value to each key of the associative array (f. i. : theList['cat'] = 1;) else it is not working as the value of "theList['cat']" is undefined, thus "if (theList[txt])" always returns false.

Thanks anyway.

I tried calling the function 'contains' and got errors (possibly a reserved word)

In the subject I wrote, "contains()" refers to the Java (not Javascript) method of the "List" interface.

[B]

Vincent Puglia
04-16-2004, 07:56 AM
it is not working

if (theList[txt]==null) alert(txt)

Vinny

b.mic
04-16-2004, 10:20 AM
Sorry, this doesn't work either as "theList[txt]==null" is always true ... :)

[B]