views:

130

answers:

3

My JavaScript code stores a lot of data in arrays. I want to retrieve a key using something similar to what I wrote below. It key that should be retrieved is based on variables that are page-dependent . The following code doesn't work. Can anyone give me a solution to this problem?

This is part of a script that does automatic conjugation. (looks for SUBJECT in a div and then looks for VERB in another div and then conjugates the verb by retrieving the conjugated form from the array)

function getarray(Array,Key) {
            return Array[Key];
        }

Example of how it should work:

verb = innerhtmlfromdiv;
subject = innerhtmlfromotherdiv;
    function getarray(Array,Key) {
           return Array[Key]; }
conjugatedverb = getarray(verb,subject);
htmltextbox.value = conjugatedverb;
A: 

Try changing the parameter name Array to something else. Array is the name of a built-in function/object in javascript.

Xavi
Silly me. Forgot that it was a keyword. *Hits head against wall*Cheers mate!
ASG
Actually, `Array` is a function, not a keyword.
SLaks
Ahh yes, you're right. I'll fix my answer.
Xavi
That shouldn't make any difference. If you make "Array" a parameter it shadows the reference to the built in Array, and becomes whatever you passed in. I think the problem here is subtler.
Breton
Totally agree with @Breton, `Array` will be shadowed within the scope of the function, the name change doesn't make any difference, unless you try to use the `Array` Constructor inside that function of course...
CMS
At the very least you'd agree that it's bad practice to name a variable `Array` or `Object`. It's hard debugging a program when you don't have the full source. All I can do is offer a piece of advice.
Xavi
If only to avoid confusion like this, yes, but nevertheless, the names in this case are superficial, and I don't believe that is the root cause of the issue.
Breton
+1  A: 

First off, what you want is an Object, not an Array. I'm guessing that you're new to javascript and your previous language was either PHP or PERL, and so you think what you're using is an "Associative Array".

The basics: There is no such thing as Associative arrays in Javascript. There is Objects, and a non-primitive subclass of Object called Array, which has some methods for dealing with Numericly named object properties, and a magic length property.

Since the keys you are dealing with are strings, not numbers, you have no use for Arrays.

Javascript Objects on the other hand are similar to an Associative array in php, or a hash in perl. (but they are not exactly the same thing).

As you have no doubt discovered, with an Object, you can use subscript notation to access certain properties, as in

verbs["go"] = "went";

this is equivilent to

verbs.go = "went";

a common mistake is to think that the dot notation is only used for objects, and the subscript notation for "associative arrays", because this is how it works in PHP. In javascript the two notations are interchangable. Since Arrays are a subclass of Object, the above examples work on them as well (but they don't use any special properties of Arrays).

As for your specific problem:

You need an object full of objects.

so for instance

var verbs = {
   "do":{"Truck":"Drive","Blender":"Turn On","Bike":"Ride"},
   "take":{"Money":"Steal","Julie":"Accompany","Lever":"Pull}

}

then your function would be:

 function conjugate (verb, subject) {
         return verbs[verb][subject];
 }

and an example of its use would be

 conjugate("do","Truck") // returns "Drive"
Breton
A: 

I don't quite get the point of the function. This is like writing:

function getValue(var) {return var}

Why not just get the value the normal way without wrapping it in a useless function:

conjugatedverb = verb[subject];
htmltextbox.value = conjugatedverb;

Also, your code doesn't make sense when you claim to do an innerHTML from an element and somehow get an object instead of a string. What is really going on? I think your problem starts even before this snippet of code.

slebetman
You might want to make it a function to abstract away the precise mechanics of how one obtains the conjugate verb. Nevertheless I share your confusion over the matter.
Breton