Deleting from the middle of an array

is there a way to delete from the middle of an array?

like if i wanted to take bananas off grocery

  
grocery = ["apples", "peach", "bananas", "mangos", "pears", "tomatoes]

what’s the code to use?


grocery.splice(2,1);

It permanently removes it though, so watch out.

edit: incase you don’t know, it’s 2 and not 3 because the array index starts at 0

ie: 0->apples, 1->peach, 2->banannas, …

ic, and to add? (since we’re on the topic)

also use splice. Look it up in Flash help. learn it. love it. use it.

That should be,


grocery.splice (2, 1);

Just using splice(2) will remove all the items from the array starting from bananas index 2. 1 indicates that you only want to remove only one item.

lol, thx sen :stuck_out_tongue:

I’ve encountered a prob:

  
grocery = ["apples", "peach", "bananas", "mangos", "pears", "tomatoes"];
trace(grocery);
//returns "apples, peach, bananas, mangos, pears, tomatoes"
grocery.splice(2)
trace (grocery)
//returns "apples,peach"

I only wanted to take out bananas ONLY…

Yeah, you’re totally right, sorry about that… I’ll edit my post for future peoples sake. This will fix your problem too vince.

yea…is there a way to only take out bananas…only!? :stuck_out_tongue:

yeah, splice(2,1);

the first number 2 tells which index to delete, and the second number tells how many to delete. Sorry I messed you up in my first reply :frowning: Hopefully all is better now.

a loop

no probs! Thx all…haha…

a loop, sen?:h:

I think it must be a joke, but I’m just too dense (/tired, it’s 5:51 a.m. and no sleep for me :frowning: ) to get it.

What are u doing up so late, man! Get to sleep!! :z:

Helping you!!! Well… failing at helping you :frowning:

lol…people forget! :wink:

I got this question from this thread actually:

http://www.kirupaforum.com/forums/showthread.php?t=66381

And look who was there :stuck_out_tongue:

there should be a tutorial about this…or added to the already-existing array tutorial…but i don’t know how to use PHP :(…hehe :stuck_out_tongue:

haha, yep… I’m just kind of hanging out waiting for new posts :wink:

I think I’m going to take a break though, maybe do some ASing.

If you hit F1 (I think, or Help>ActionScript Dictoinary, or something) you can get the Flash help, searching the AS dictionary for Array and such is really the best way to learn.

LOOP!!! If you’re working with arrays, you have to know (well should know) how to loop.

// ActionScript:
function RemoveLikeElements(_array, _element){
	var i = _array.length;
	while (i--) if (_array* == _element) _array.splice(i,1);
}

grocery = ["bananas", "apples", "peach", "bananas", "mangos", "pears", "bananas", "tomatoes"];
trace(grocery);
// bananas,apples,peach,bananas,mangos,pears,bananas,tomatoes
RemoveLikeElements(grocery, "bananas");
trace(grocery);
// apples,peach,mangos,pears,tomatoes

lol! sen, you’re post helped me understand about functions more than anything :thumb: :stuck_out_tongue:

ah, i see… remove like elements in one fell swoop.