Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Changing Array Number

7 views
Skip to first unread message

Michael Stemper

unread,
Apr 22, 2003, 1:36:26 PM4/22/03
to
In article <3e9a6a78$1...@usenet.per.paradox.net.au>, "Adam" <adams...@hotmail.com> writes:
>Say I have an array like below:
>
>ArrayName[1] = "Text Here 1"
>ArrayName[4] = "Text Here 4"
>ArrayName[8] = "Text Here 8"
>ArrayName[9] = "Text Here 9"
>
>Is there a simple way to get these arrays so they are like:
>
>ArrayName[1] = "Text Here 1"
>ArrayName[2] = "Text Here 4"
>ArrayName[3] = "Text Here 8"
>ArrayName[4] = "Text Here 9"

Try this:

<script language="JavaScript">
var dest=0;
NewArray = new Array();
for( src in ArrayName )
{
NewArray[dest++] = ArrayName[src];
}
ArrayName = NewArray;
</script>

This will pack all entries into the array, starting with the first
slot in it. If you really want to skip the first slot and begin with
the second (as indicated in your post), replacing "dest++" with "++dest"
should do the trick.

--
Michael F. Stemper
#include <Standard_Disclaimer>
The FAQ for rec.arts.sf.written is at:
http://www.geocities.com/evelynleeper/sf-written.htm
Please read it before posting.

Evertjan.

unread,
Apr 22, 2003, 2:17:36 PM4/22/03
to
Michael Stemper wrote on 22 apr 2003 in comp.lang.javascript:

> In article <3e9a6a78$1...@usenet.per.paradox.net.au>, "Adam"
> <adams...@hotmail.com> writes:
>>Say I have an array like below:
>>
>>ArrayName[1] = "Text Here 1"
>>ArrayName[4] = "Text Here 4"
>>ArrayName[8] = "Text Here 8"
>>ArrayName[9] = "Text Here 9"
>>
>>Is there a simple way to get these arrays so they are like:
>>
>>ArrayName[1] = "Text Here 1"
>>ArrayName[2] = "Text Here 4"
>>ArrayName[3] = "Text Here 8"
>>ArrayName[4] = "Text Here 9"
>
> Try this:
>
> <script language="JavaScript">
> var dest=0;
> NewArray = new Array();
> for( src in ArrayName )
> {
> NewArray[dest++] = ArrayName[src];
> }
> ArrayName = NewArray;
> </script>
>

Without a loop is quicker.
Array must not have '~'s

<script language="JavaScript">
ArrayName=new Array


ArrayName[1] = "Text Here 1"
ArrayName[4] = "Text Here 4"
ArrayName[8] = "Text Here 8"
ArrayName[9] = "Text Here 9"

ArrayName =
ArrayName.join('~').replace(/~+/g,'~').replace(/^~/,'').split('~')

alert(ArrayName)
</SCRIPT>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Svend Tofte

unread,
Apr 22, 2003, 2:55:54 PM4/22/03
to
On 22 Apr 2003 18:17:36 GMT, Evertjan. wrote:

> Without a loop is quicker. Array must not have '~'s

> ArrayName = ArrayName.join('~').replace(/~+/g,'~').replace(/^~/,'')
> .split('~')

Wouldn't it be kinda slower? String operations are usually fairly
"intensive", and only that, but you run two whole regex runs, requiring the
regex parser to startup. Anyway, it's shorter, but is that better? I'm not
sure, as I dont know the internals of JavaScript, but I'd go with the loop
personally... :)

Svend
stud.useless.scient

0 new messages