What I am trying to do
I am trying to access several arrays by passing a parameter to a function that is basically controls a slideshow. The parameter I pass to the function is the nameof a <a href=#" name="rebounding" onclick="nextPlayer(this.name)"></a>. Here is one of the arrays I am trying to access.
var reboundingImgs = ['tyson_chandler.png', 'dwight_howard.png', 'zach_randolph.png', 'omer_asik.png', 'nikola_vucevic.png', 'joakim_noah.png'];
The problem
When I pass the parameter to the function that changes the image and I use the [index]instead of getting the index in the array called reboundingImgs[]I get the letter in the string parameter. e.g. parameter + "Imgs"[index] would be "b" if index was 2 and the parameter I pass in is "rebounding". I need to access the arrays using the parameter I pass in to show the right image from the right array( I have several arrays containing images e.g. scoringImgs[], reboundingImgs[], assistsImgs[], etc)
Here is my code
function nextPlayer(typeStatStr) {
var numImgs = 6;
var img = document.getElementById("slideImage");
var imageName = img.name.split("_");
var index = imageName[1];
if (index == numImgs -1) {
return;
}
else {
index++;
}
img.src = "..\\nbaart\\" + typeStatStr + "Imgs"[index];
img.name = "image_" + index;
}
function prevPlayer(typeStatStr) {
var img = document.getElementById("slideImage");
var imageName = img.name.split("_");
var index = imageName[1];
if (index == 0) {
return;
}
else {
index--;
}
img.src = "..\\nbaart\\" + typeStatStr + "Imgs"[index];
img.name = "image_" + index;
}
3 Answers 3
Make an object which has properties that you can access given the name of the arrays:
var imgs = {};
imgs.rebounding = ['tyson_chandler.png', 'dwight_howard.png',
'zach_randolph.png', 'omer_asik.png',
'nikola_vucevic.png', 'joakim_noah.png'];
alert(imgs["rebounding"][0]); // tyson_chandler.png
3 Comments
name. This method is much more object-oriented than Juan Mellado's however, attaching data to window that could easily be in its own variable is bad practice.Why don't pass the proper array as a parameter to the function?
nextPlayer(reboundingImgs);
And then just:
typeStatStr[index];
Anyway, if the arrays are some sort of global variables (window scope?) then you can do this:
window[typeStatStr + "Imgs"][index];
1 Comment
nextPlayer() and prevPlayer() respectively. I also tested your second suggestion window[typeStatStr + "Imgs"[index]; and it worked good, but since it only works with global variables I decided to not use it.String is just type character array so by doing: "Imgs"[index]; you are accessing the index-th element of the array of "Imgs". For instance "Imgs"[2] == 'g'.
If you want to access the index-th element of the array Imgs then you should do:
Imgs[index]; //Without the quotes
2 Comments
typeStatStr, which is a string, with another string "Imgs" so that it forms the name of an array and then access that array with [index].
<head>I do it like this so the browser can pre load them into memory.