# Deleting from an array

**URL:** <https://forum.kirupa.com/t/deleting-from-an-array/14226>\
**Category:** flash\
**Created:** [February 23, 2003, 8:44pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226 "2003-02-23T20:44:47Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Big\_Al](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@Big\_Al](https://forum.kirupa.com/u/Big_Al)\
**Post date:** [February 23, 2003, 8:44pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/1 "2003-02-23T20:44:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 8:49pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/2 "2003-02-23T20:49:04Z")

</div>

using pop()

```php

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 8:54pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/3 "2003-02-23T20:54:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 8:57pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/4 "2003-02-23T20:57:31Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 9:00pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/5 "2003-02-23T21:00:43Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 9:06pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/6 "2003-02-23T21:06:00Z")

</div>

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

```php

// 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  
🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 9:09pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/7 "2003-02-23T21:09:53Z")

</div>

Simplest one I can create…

```php
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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 9:23pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/8 "2003-02-23T21:23:23Z")

</div>

Ahmed yours out puts this:

```php

Array = 1,,3

```

I kept having the same problem.

Nice one Blue. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 9:25pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/9 "2003-02-23T21:25:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 9:29pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/10 "2003-02-23T21:29:51Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 9:35pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/11 "2003-02-23T21:35:30Z")

</div>

so this is the final thing?

```php

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

```

=)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 9:38pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/12 "2003-02-23T21:38:45Z")

</div>

Yup… That should do the job… =)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 9:55pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/13 "2003-02-23T21:55:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 10:11pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/14 "2003-02-23T22:11:42Z")

</div>

A prototype could be nice too 🙂

```auto
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

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

```

pom :phil:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 10:32pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/15 "2003-02-23T22:32:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 10:46pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/16 "2003-02-23T22:46:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 11:00pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/17 "2003-02-23T23:00:50Z")

</div>

Nice job, bro… =)

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 11:05pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/18 "2003-02-23T23:05:18Z")

</div>

thank you

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 11:09pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/19 "2003-02-23T23:09:12Z")

</div>

😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 2, 2003, 6:02pm UTC](https://forum.kirupa.com/t/deleting-from-an-array/14226/20 "2003-03-02T18:02:38Z")

</div>

i found this prototype over at [downloads.junioronline.us](http://downloads.junioronline.us) … it pretty much does the same thing but in a more interesting way… and its a prototype… 🙂

```php

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

```

[Next page](https://forum.kirupa.com/t/deleting-from-an-array/14226.md?page=2)
