I would like to take the value of an li ID attribute (which will be a userID), and use it as part of a string that I will eventually use as part of a variable name. I will use this variable name to create an array with.
I understand the basics, but can't seem to find the right combination of jQuery/javascript to make this magic happen.
jQuery('#user-list li').click(function() {
var userID = jQuery(this).attr("id");
// i want to add the word array to the end of userID
var theVariableName = userID + "Array";
// I want to use this variable to create an array
var theVariableName = new Array();
// I want to continue to use the name throughout my document
theVariableName.push({startTime: 7, endTime: 10});
alert(theVariableName[0].startTime);
});
解决方案
Use an Object to hold the various user arrays:
window.userData = {};
$(...).click(function() {
// ...
window.userData[userID] = [];
window.userData[userID].push({startTime:7, endTime:10});
alert(window.userData[userID][0].startTime);
}
You might not want to store the userData object in the global namespace though; to prevent accidental name conflicts, you should at least put it in your own namespace.