create Array dynamically?
Is it possible to create a javascript array with an arbitrary name on the fly? I don't quite see how I can do this.
the goal is to create a function that takes a path to a folder, iterates through the folder and 3-5 layers of subfolders, and puts the paths of all files in those folders into an array. Ideally, this array could be made on the fly, so I don't have to preallocate a LOT of arrays at the start of my js code. That would be messy, not to mention limiting!
to show you how much I don't understand, here is a code snippet that of course doesn't work:
function newone(v,a)
{
v = new Array();
v[0]=a;
}
the message "newone jones 2" just adds the value 2 to the array "v".
peter
maybe you could do something with a "2 dimensional" array.
v[v.length + 1] = new Array();
sort of thing.....
Then every time you need to create a new array you could just keep appending new arrays in v
-A
thanks andrew, that seems to work nicely!
example patch:
-----test1.js--------
v = new Array();
function makenew(x)
{
v[x] = new Array();
}
function vary(x,n,b)
{
var jones = new Array();
jones = v[x];
jones[n] = b;
}
function dump(n)
{
var temp = new Array();
temp = v[n];
post(n," :",temp[0],temp[1],temp[2],"n");
}
-------end test1.js----------
Plan B - the javascript eval() function:
function addToArray()
{
a = arrayfromargs(arguments);
if(eval("!" + a[0])) // test for non-existence
{
eval(a[0] + " = [" + a.slice(1) + "]");
}
else if(eval("typeof(" + a[0] + ".slice) == "function"")) // test for array
{
eval(a[0] + " = " + a[0] + ".concat(" + a.slice(1) + ")");
}
else
{
post(a[0] + ": not an arrayn");
return;
}
post("array " + a[0] + ": ");
eval("post(" + a[0] + ")");
post("n");
}
function clearArray(aname)
{
if(eval("typeof(" + a[0] + ".slice) == "function"")) // test for array
{
eval(aname + " = []" );
post("array " + aname + ": clearedn");
}
else
{
post(a[0] + ": not an arrayn");
}
}
cool...the eval function is new to me!
but I guess what I meant is how can i initialize an array dynamically. These are for manipulation of an existing array, correct?
p.
eval() can do anything you want dynamically. this is the big bonus of
an interpreted language. go nuts. i sure do. there are user-code
insertion points all over my app. ;)
Using eval in this way will probably be noticeably slower, FWIW. I
would recommend exploiting object properties rather than John's
example code, though sure, eval is useful in many contexts (see
dynexpr.js example) when you need true dynamism.
-Joshua
Quote: pnyboer wrote on Sat, 10 June 2006 12:57
----------------------------------------------------
> These are for manipulation of an existing array, correct?
No, the code will create the array for you if it doesn't already exist, for both the add and clear functions.
Also does some checking (for presence of the the array.slice function) to ensure that you're not overwriting existing variables that aren't arrays - this is pretty much essential when using eval() to process input. You could just make a function that eval()s any symbol you poke at it and send it line after line of js from the outside world, but that could definitely be considered a little ... unwise.
As Joshua notes, eval() is not fast - I'd be wary of using this for anything realtime, I tend to use eval() for dynamic patch/variable setup, processing js-based preference files, etc.