Dynamically creating and naming arrays in javascript
I may be looking at this one the wrong way. I think that dynamically naming the object is the difficult part rather than creating it.
Basica开发者_Python百科lly I have an array of arrays and I want to get the nested arrays out.
var $jTableTRS = $('tr.child');
var $jTableTRSArr = jQuery.makeArray($jTableTRS);
each slot in $jTableTRSArr has an array object contained. Thing is i will never know how many arrays are in the $jTableTRSArr.
for(var i=0;i<$jTableTRSArr.length;i++)
{
//var tempArray(withuniqueidentifier) = $jTableTRSArr[i]
}
This means I could reference each nested array by its name like
tempArray1[0].variable
and not $jTableTRSArr[0].variable.
I'm essentially bringing the nested arrays up one level.
Can this be done or is there a better way to do it?
To create dynamic variable names, you could do:
for (var i = 0; i < arr.length; i++){
window['MyNewArray-' + i] = arr[i];
}
Is this what you were looking for?
精彩评论