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.
year_choice['2006'] = ...';