From: MikeJ on
Need to know how to clear a global array so Array.length will = 0
(meaning no indexes). Cant find the information in the book.
Thanks
Mike
From: Sean Kinsey on
On Apr 27, 11:05 pm, MikeJ <no_spam_ple...(a)nothere.com> wrote:
> Need to know how to clear a global array so Array.length will = 0
> (meaning no indexes). Cant find the information in the book.
> Thanks
> Mike

If the Array is only referenced by a single global variable then the
easiest way is to just replace the reference to with that of a new
Array
var globalArray = [foo, bar];

//reset
globalArray = [];

Alternatively you can use splice which adds/removes elements from the
array
globalArray.splice(0, globalArray.length - 1);
(not sure if '- 1' is needed here)

From: Stefan Weiss on
On 27/04/10 23:05, MikeJ wrote:
> Need to know how to clear a global array so Array.length will = 0
> (meaning no indexes). Cant find the information in the book.

To truncate an array so that arr.length = 0, you need to write

arr.length = 0;

I know, weird and unexpected syntax, but there it is ;)


--
stefan
From: Stefan Weiss on
On 27/04/10 23:17, Sean Kinsey wrote:
> On Apr 27, 11:05 pm, MikeJ <no_spam_ple...(a)nothere.com> wrote:
>> Need to know how to clear a global array so Array.length will = 0
>> (meaning no indexes). Cant find the information in the book.
>> Thanks
>> Mike
>
> If the Array is only referenced by a single global variable then the
> easiest way is to just replace the reference to with that of a new
> Array
> var globalArray = [foo, bar];
>
> //reset
> globalArray = [];

Strictly speaking, that doesn't "clear" the array, it just assigns the a
new empty Array to the variable globalArray:

var arr = [1, 2];
var alias = arr;

arr = [];
print alias.length; // 2 -- whoops!


--
stefan
From: Stefan Weiss on
On 27/04/10 23:27, Stefan Weiss wrote:
> print alias.length; // 2 -- whoops!

That should read print(alias.length), or alert(..), or similar.


--
stefan