View Full Version : Deleting from an array
Big_Al
February 23rd, 2003, 03:44 PM
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.
Jubba
February 23rd, 2003, 03:49 PM
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...
Big_Al
February 23rd, 2003, 03:54 PM
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?
Jubba
February 23rd, 2003, 03:57 PM
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..
Big_Al
February 23rd, 2003, 04:00 PM
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...
ahmed
February 23rd, 2003, 04:06 PM
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[i] == entry)
{
arrayname[i] = ""
break;
}
}
let me know if it works
:)
CyanBlue
February 23rd, 2003, 04:09 PM
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[i] == str)
return i;
}
return -1;
}
Before : Banana Peel,Hippopotamus Leg,Computer Wire,Keyboard
After : Banana Peel,Hippopotamus Leg,Keyboard
Jubba
February 23rd, 2003, 04:23 PM
Ahmed yours out puts this:
Array = 1,,3
I kept having the same problem.
Nice one Blue. :)
ahmed
February 23rd, 2003, 04:25 PM
oh.. i thought the intention was just to empty the array entry.. misunderstood it =).. and yeah.. nice one blue :P
CyanBlue
February 23rd, 2003, 04:29 PM
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[i] = "" line should be arrayname.splice(i, 1); to properly delete the item from the array...
ahmed
February 23rd, 2003, 04:35 PM
so this is the final thing?
function deleteEntry(arrayname, entry)
{
n=arrayname.length
for(i=0; i<(n+1); i++){
if (arrayname[i] == entry)
{
arrayname.splice(i, 1);
break;
}
}
=)
CyanBlue
February 23rd, 2003, 04:38 PM
Yup... That should do the job... =)
Big_Al
February 23rd, 2003, 04:55 PM
well, thanks to all who have been so kind as to help me =)
pom
February 23rd, 2003, 05:11 PM
A prototype could be nice too :)
Array.prototype.deleteEntry=function(entry) {
var n=this.length;
for(i=0; i<(n+1); i++){
if (this[i] == entry){
this.splice(i, 1);
break;
}
}
return this;
}Or even
Array.prototype.deleteEntry2=function(entry) {
for(var i in this){
if (this[i] == entry){
this.splice(i, 1);
break;
}
}
return this;
}pom :phil:
Big_Al
February 23rd, 2003, 05:32 PM
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
pom
February 23rd, 2003, 05:46 PM
I believe that the data of a SO is an object, so you can store pretty much anything in there.
CyanBlue
February 23rd, 2003, 06:00 PM
Nice job, bro... =)
Yes... You can add array into the Shared Object with no problem...
Big_Al
February 23rd, 2003, 06:05 PM
thank you
CyanBlue
February 23rd, 2003, 06:09 PM
;)
ahmed
March 2nd, 2003, 01:02 PM
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... :)
Array.prototype.removeItem = function(item) {
for (var n = 0; n<this.length; n++) {
if (this[n] == item) {
this.splice(n, 1);
return;
}
}
};
CyanBlue
March 2nd, 2003, 06:04 PM
Howdy...
Just out of curioisty... =)
Um... I do not see much difference between the prototype you have proviided and the one ilyaslamasse has provided up there... What did you mean by 'more interesting way'??? Maybe I am missing something here??? :cowboy:
ahmed
March 2nd, 2003, 06:44 PM
Originally posted by CyanBlue
Maybe I am missing something here??? :cowboy: nah.. its just two lines shorter :P.. basically they're the same actually.. sorry for any confusion i might have caused :)
CyanBlue
March 2nd, 2003, 06:49 PM
Nah... Don't be... You were just trying to help... =)
ahmed
March 2nd, 2003, 06:52 PM
=)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.