PDA

View Full Version : Array loaded from a string?


blindbull
09-18-2002, 04:05 PM
Is there a statement in JavaScript that is similar to the redim preserve statement in Visual Basic?

I am trying to write a routine to load an array with a string that contains data e.g.

strMyString = '12,44,6,63,2,'

To load this into an array I can create a new array and dimension it to be the length of the string. Then I can add to the array as follows:

do
{
commapos = selectedPackages.indexOf(",",1)
strListFrom = selectedPackages.substr(0,commapos);
commapos = commapos + 1;
selectedPackages = selectedPackages.slice(commapos,strLen);
strListFrom = selectedPackages.substr(0,commapos-1);
strMyString[noOfCommas] = strListFrom;
noOfCommas++;
}
while (commapos != 0)

Then I can remove all the elements that are null by looping to the length of the array.

But surely there is a better way? If I could use a statement like the redim preserve one I could have the array the correct dimension for the data.

mordred
09-18-2002, 04:52 PM
strMyString.split(",");

blindbull
09-19-2002, 09:19 AM
I like that very much!

It reduced my code to two lines.

Thanks.