views:

21

answers:

2

I have a problem with populating an autocomplete list based on a previous input.

Basically, I have about 40 or so different arrays containing car models and I want to populate the autocomplete list based on the previous input. What I want to do is:

  1. Get the value of the previous input (the name of this is the same name as one of the arrays)
  2. Select the correct array based on this input.
  3. Pass this array into the autocomplete function.

The problem is that I don't know how to select an array based on a string. Can anyone suggest a solution?

Thanks.

A: 

I wouldn't start with 40 different arrays in 40 variables, I would put them all into a single object:

var cars = {
    'Ford' : ['Mustang', 'Prefect'],
    'Toyota' : ['Corolla', 'Corona']
};

You can then select the correct one like so:

cars.Ford
// or
cars['Ford']
// or, more likely how you'll use it:

var make = someSelectBox.value;
cars[make]
nickf
This came to me just as soon as I had completed writing the arrays - d'oh!Say I wanted to select the 'Ford' array thought? How is it done?
Gearóid
Excellent! Thanks so much!
Gearóid
A: 

I think you're on a much more profitable track with nickf, but you could take advantage of $.map(). It'd be something like:

testArray = $.map(oldArray, function(n){
  return ( n == 'string' );
});

And then, if testArray has anything in it, bam.

D_N
Step 1: javascript objects. Step 2: **nickf**. Step 3: Profit! yessss
nickf
Haha! Yeah it's true.
D_N