Deleting from an array

Hi there, its me with another array question.

Is there a way to delete a specific value in an array? For example, the array:

“Banana Peel,Hippopotamus Leg,Computer Wire,Keyboard”

Would there be any way to delete just “Computer Wire” from the array? I found the Array.splice thingy, but it seems that you need to know the index of the thing. Therefore i guess my question is: Is there any way to find the index of a particular value.

In the thing I am making, the value could be at any index in the array (it is put in place by the user), so I could not just say Array.splice(2,1,). Any ideas?

Thanks.

using pop()


myArray1 = ["1", "2", "3"];
myArray1.pop ();
trace ("myArray1 = " + myArray1);

// Outputs:
// myArray1 = 1,2

well POP deletes the last thing added to the array…

shift deletes the first thing added to the array…

as far as deleting something inside… you’ll have to make a function up…

but doesn’t using pop() just take out the last value in an array? I was looking for something that would delete a specific value that could be anywhere in the array, not just in the last space.

For example, an array:

a,r,s,h,p,g,m,n,d

Is there any way that a script could find and delete only the “h”, rather than only delete the last or first part of the array? Or is there some way to move a value to the last part of the array and then use pop() to delete it?

as far as deleting something inside… you’ll have to make a function up… better wait for sen or someone… I am heading out… sorry…

oops, didnt see the “youll have to make a function up” part. Now i am stuck on that. I dont know of any ways to find a value in an array. or did you mean something else…

i dont have the time to test it out… but i think this should do it


// the name of your array goes in arrayname, and the text that you want to be deleted goes in entry
function deleteEntry(arrayname, entry) 
{
n=arrayname.length
for(i=0; i<(n+1); i++){
if (arrayname* == entry) 
{
arrayname* = ""
break;
}
}

let me know if it works
:slight_smile:

Simplest one I can create…

tempArray = new Array("Banana Peel", "Hippopotamus Leg", "Computer Wire", "Keyboard");
trace("Before : " + tempArray);
tempArray.splice(getItem("Computer Wire"), 1);
trace("After  : " + tempArray);
function getItem(str)
{
	for (i = 0 ; i < tempArray.length ; i++)
	{
		if (tempArray* == str)
			return i;
	}
	return -1;
}

Before : Banana Peel,Hippopotamus Leg,Computer Wire,Keyboard
After : Banana Peel,Hippopotamus Leg,Keyboard

Ahmed yours out puts this:


Array = 1,,3

I kept having the same problem.

Nice one Blue. :slight_smile:

oh… i thought the intention was just to empty the array entry… misunderstood it =)… and yeah… nice one blue :stuck_out_tongue:

Well… Not really nice one, I guess… That method will work just fine if I have small number of elements in the array… But think about the case when you have to traverse a couple thousand elements to find one and that one happened to be at the end of the array???

ahmed’s arrayname* = “” line should be arrayname.splice(i, 1); to properly delete the item from the array…

so this is the final thing?


function deleteEntry(arrayname, entry) 
{
n=arrayname.length
for(i=0; i<(n+1); i++){
if (arrayname* == entry) 
{
arrayname.splice(i, 1);
break;
}
}

=)

Yup… That should do the job… =)

well, thanks to all who have been so kind as to help me =)

A prototype could be nice too :slight_smile:

Array.prototype.deleteEntry=function(entry) {
	var n=this.length;
	for(i=0; i<(n+1); i++){
		if (this* == entry){
		this.splice(i, 1);
		break;
		}
	}
	return this;
}

Or even

Array.prototype.deleteEntry2=function(entry) {
	for(var i in this){
		if (this* == entry){
			this.splice(i, 1);
			break;
		}
	}
	return this;
}

pom :phil:

just one more quick question…

is it possible to store an array in a flash shared object, or will it store as a variable and then I would have to convert it back into an array when I get it out of the SO?

i think there was a thread or something on this site about converting variables that were once arrays back into arrays…

thanx

I believe that the data of a SO is an object, so you can store pretty much anything in there.

Nice job, bro… =)

Yes… You can add array into the Shared Object with no problem…

thank you

:wink:

i found this prototype over at downloads.junioronline.us … it pretty much does the same thing but in a more interesting way… and its a prototype… :slight_smile:


Array.prototype.removeItem = function(item) {
     for (var n = 0; n<this.length; n++) {
          if (this[n] == item) {
               this.splice(n, 1);
               return;
          }
     }
};