0

I have 10 arrays. They are named resultsYEARobject where 2002 <= YEAR <= 2011. I would like a way to call each array based on another variable. To give a bit more context, I have a drop down menu where the user can pick a year they would like to search (2002-2011). When they hit a "search" button, I want to be able to search the result array that pertains to that year.

This is what I'm trying to accomplish (in PSEUDOCODE).

    var year_choice = 2002;
    var array_to_search = ("results" + year_choice + "object"); 
    //array_to_search would be "results2002object"

    for(int i = 0; i < array_to_search.length; ++i) {
        //do stuff
    }

I've been programming long enough to know that doesn't make any logical sense and no program would properly compile the code. That said, is there another, proper, way I can accomplish what I'm trying to do?

EDIT

getScript(file_path, function(){
alert("found and loaded file " + file_path);

//refine for the parameter
var search_results = new Array();
for(var i = 0; i < results2002objects.length; ++i) { //go through the array in the file I want (dependant on what the user chooses in the drop down menu)
    if(results2002objects[i].sub == selected_parameter) {
        search_results.push(results2002objects[i]); // add the object to the array
    }
}

alert("found " + search_results.length + " results with the parameter " + selected_parameter);
for(var g = 0; g < search_results.length; ++g) {
        $("#right_pane_results").append(search_results[g].st_id);
}
});

So after I load the file I go through the array like that. The issue is no matter the year the user picks, the above code will go through results2002object. Also, I don't have much control over the key names (array['whatever'] as the data was parsed using a Java program I made and I used the GSON library to convert it to JSON.

5
  • 3
    Why don't you store them in a object with the key as the years Commented Jan 20, 2014 at 2:48
  • @thefourtheye I have each year as a separate JavaScript file (the data comes from somewhere else) and I wanted each file separately to reduce loading times at the beginning. The 2002 file is 30MB and everything is 300MB. Commented Jan 20, 2014 at 2:53
  • @thefourtheye So when the user searches, I load that specific file for the year and go through the array. Commented Jan 20, 2014 at 2:54
  • 1
    @user3210944: if you load the specific file for the year and go through the array, I don't see why you would need to select the specific array since there would be only one, representing the parsed file. Commented Jan 20, 2014 at 2:58
  • @user3210944—you can still use that strategy. When the user needs say 2006, load the script file that has year_choice['2006'] = ...'; Commented Jan 20, 2014 at 3:00

3 Answers 3

2

Why not just make an object and add elements dynamically?

var year_choice = {}
var year_the_user_selects = "2002";
year_choice[year_the_user_selects] = array_you_want_here;
Sign up to request clarification or add additional context in comments.

4 Comments

See my reply to thefourtheye. I have each array in a separate file and load it when I need to.
Yeah, 30MB is big! Haha. Could I see the code for the file loading?
It can also be an array: var years = []; years['2002'] = ...; years['2003'] = ...; etc. so there is convenient access to Array methods if useful.
@AnalogWeapon I have added some code in the edit. If you need more let me know.
1

So you have the following arrays:

results2002obj, results2003obj, ..., results2012obj

And you'd like to access them AS IF they were an array.

You can use the "self" namespace. For example, results2002obj is the same as self.results2002obj

It's also worth noting that in JavaScript, a.xyz is the same as a['xyz']

What this means is that you can treat any variable name as if it's a key in an array:

function get_results(year){
    return self['results'+year+'obj'];
}

Hope this helps.

2 Comments

Wow that just confused the heck out of me. But I do think this will work. Thanks. I will try it tomorrow and give you accepted answer if it works.
Thanks that works. And it does make a lot of sense.
0

Do not use numeric keys in the array, as it'll create 0, 1, 2, all the way up to 2002.

Declare a map like this:

var choices={
'year_2002':['option 1', 'option 2', 'option 3'],
'year_2003':['option 1'],
'year_2004':['option 1','option 2'],
...
};

//pick a year:
var year=2003;
var options = choices['year_'+year];

4 Comments

I don't think this will work for me. I have each year in a separate file.
Your pseudo code is very abstract. I don't know how you intend to use the data, and how much control you have over the format the data, and how the plumbing works. All these factors affect the formulation of your program. Mind tell more? Also with that much data, the files should be indexed in a database and served by a server-side script.
Sorry. I didn't want to give too much information but I guess it was needed. I have added some of my code in an edit. And yes, ideally it would be in a database but I don't have time to change things now. This is due tomorrow.
added another answer as the new answer is different from the original one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.