Possible Duplicate:
Remove item from array by value
I am maintaining string lists like
var keyString = [];
keyString.push("anotherString");
keyString.push("yetAnotherString");
keyString.push("oneLastString");
I want to be able to return all results from keyString less a value I already know about.
For example, if I have anotherString, then I want to return everything in the array that isn’t anotherString.
Obviously this can be done a few ways easily, but I have some restrictions.
I don’t want the solution to use any loops, and I don’t want to use excessive amounts of memory, and I don’t want to modify the original array.
This may be impossible, but I thought I would throw it out there and see if anything exists.
Some possibilities considering your example code, but inside a function (or
returnwouldn’t make sense). The examples assume you’re fine with modifying the original array, since you don’t want to copy.1. Using
shiftto remove the first element2. Using
popto remove the last element3. Using
spliceto remove the middle element