views:

93

answers:

1

Overview:
I have an array of 20 byte strings that needs to be stored on a web page for use in user entry validation. I anticipate between 25 and 1000 elements in that array.

Considerations:
1. The web client will be a mobile device with reduced memory and processor capability.
2. I am limited to client-side validation only (technical limitation for us right now).
3. Security is not an issue - I understand that the user can view the source.
4. I do not want to show the array to the user. I only want the user to enter a value, and then I iterate over the array to see if there's a match. If no match, I will issue a javascript message box to the user.

Question:
What is the best way to store and iterate over this data?

+1  A: 

Instead of iterating, use an associative array, and test for the existence of that element:

var myChunks=[]
myChunks['aabbccdd...'] = true

Then you can check for the existance of the entered value by a simple test. No loops required:

if(myChunks[myValue]) {
   //found the value
}
else {
  // did not find value
}
Diodeus
Was the above example supposed to say var myChunks = {} rather than []? Seems odd to declare something as an array but treat it as a dict, although it will work.
runeh
Also, in modern browsers, it might look cleaner to use the array.indexOf method (from js 1.6). Could would be: chunks = ["foo", "bar", "baz"]; if (chunks.indexOf(something) == -1) { alert("not found!") } . See https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/indexOf
runeh
myChunks = {} is an object, myChunks = [] is the same as "new Array()"
Diodeus
Diodeus: But why declare an array when you are not using it as such? If you declare it as an object, it's clear that it's being used as an associative array. If you declare it as an array, people will expect that it's something indexed by integers, which in this case it isn't.
runeh