I have a bunch of arrays, and a bunch of folders with files I'm trying to get to. The items in the arrays correspond to the file names and the array names match the folder names.
So I have
var myArray = ['my', 'file', 'here'];
var myArray2 = ['some', 'more', 'files'];
& similar as audio files. For example `myArray/my.mp3'
I'm trying to create a string like that dynamically. I wanted to somehow convert my array names to strings but apparently that can't be done. Any other solutions much appreciated.
Not sure I understand your question. If you want flexibility, you could use an object. Initialize it at the beginning of your life-cycle app like so:
var obj = { name: 'tmp', value: null }
Later, you could assign both a name and a value to your object based on the user action:
obj['name'] = 'chosenArray';
obj['value'] = myArrayTwo;
Hope this helps,
R.
You technically CAN access a variable using a string containing its name:
var array1 = [1,2,3,4];
var array2 = [5,6,7,8];
var pick = "array2";
var chosen = window[pick];
and you can also create a global variable that way
var newvarname = "array3";
window[newvarname] = [9,10,11,12];
This works because window
in a browser is a predefined object that contains all the variables and the functions. You in addition to change and access globals using window
you can also iterate over all the global variables:
// Collect names of all globally defined arrays
arrays = [];
for (var n in window) {
if (window[n] && window[n].constructor == Array)
arrays.push(n);
}
but I can think very few cases in which you may want to do that (if you have a function or a variable named "foo" and the directory contains a file named "foo", do you really want to destroy your function/variable?).
Probably you should simply use an object to store your files
var myobj = {};
myobj[filename] = ...;
You want to use an object where property names are programatically accessible:
var audioFiles = {
"myArray": ['my', 'file', 'here'],
"myArray2": ['some', 'more', 'files']
};
You can easily loop that object, and access the arrays dynamically via bracket notation .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.