-
working with arrays
is there a quick script to clear an array?
Something like
myArray.clearArray();
would be great.
any help would be great
Matt
-
Senior Member
Hi,
you could just create a new array with the same name,
code:
var myArray = [];
-
Best way to delete items
The best way to go about this is usually to always set the value of the variable/array/etc. to null as explained by catbert303. Good advice from him :-)
-
ok, good advice.
I got it settled with a button that does this:
clear_btn.onRelease = function() {
for (i=0; i<_root.drinkArray.length; i++){
_root.drinkArray [i] = "";
}
drink_list.setDataProvider(drinkArray);
}
second question:
would
myArray[i] = [];
remove an element from my array?
would this effect the length of the array?
matt
-
Senior Member
Hi,
myArray[i] = [];
wouldn't change the size of myArray, it would just mean at position i the element was an empty array. to remove the element at position i (and reduce the length of the array by 1) you could use the splice method,
myArray.splice(i, 1);
-
that's the thing.
so,
myArray.splice(0);
would clear the whole thing and shorten the length of the array to zero?
matt
-
Senior Member
yep
-
-
i get some weird thing ahppenin whenever i use splice. the object i removed from the array, but it leaves an empty spot. after a while my array looks like [,,,,,,,,,]. why is it placing an empty spot?
-
Senior Member
what does your splice code look like? splice can be used to remove elements from an array and also insert things in their place.
-
i use a getIndex function to find its index and set it to x.
so its
var x = getIndex(this);
eArray.splice(x, 1);
-
Senior Member
what happens if you try testing a movie using,
trace("original eArray...");
trace(eArray.join("\n"));
var x = getIndex(this);
trace("x = " + x);
eArray.splice(x, 1);
trace("new eArray...");
trace(eArray.join("\n"));
-
oops, i defined getIndex as a prototype and was using it wrong.
so that mystery is solved
thanks catbert
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|