-1

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.

Community
  • 1
  • 1
user1500053
  • 79
  • 1
  • 9

3 Answers3

2

You could use filter(), but this will return a new array as well:

var newArray = keyString.filter( function( el ) {
  return el !== 'anotherString';
} );
Sirko
  • 65,767
  • 19
  • 135
  • 167
2

Some possibilities considering your example code, but inside a function (or return wouldn't make sense). The examples assume you're fine with modifying the original array, since you don't want to copy.

1. Using shift to remove the first element

function something() {
    someObject["keyString"] = [];
    someObject["keyString"].push("anotherString");
    someObject["keyString"].push("yetAnotherString");
    someObject["keyString"].push("oneLastString");
    someObject["keyString"].shift(); 
    return someObject;
}

2. Using pop to remove the last element

function something() {
    someObject["keyString"] = [];
    someObject["keyString"].push("yetAnotherString");
    someObject["keyString"].push("oneLastString");
    someObject["keyString"].push("anotherString");
    someObject["keyString"].pop(); 
    return someObject;
}

3. Using splice to remove the middle element

function something() {
    someObject["keyString"] = [];
    someObject["keyString"].push("yetAnotherString");
    someObject["keyString"].push("anotherString");
    someObject["keyString"].push("oneLastString");
    someObject["keyString"].splice(1,1); 
    return someObject;
}
bfavaretto
  • 69,385
  • 15
  • 102
  • 145
  • 1
    Just for the record, `.splice` and `.unshift` are pretty likely to be linear-time, though probably implemented with native code. (That is, they're probably "loops" internally, if that matters to the OP.) – Pointy Nov 14 '12 at 16:24
  • These won't work since I can't garauntee that its the first or last item in the array and splice modifies the original array. ( I will edit to include that as a restriction) – user1500053 Nov 14 '12 at 16:25
  • 2
    @user1500053 So you don't want to modify the original array, and don't want to copy either? I guess there's no way, then. – bfavaretto Nov 14 '12 at 16:26
  • @Pointy sorry for ask, what does OP mean. I have seen this for several times today, haven't figured out yet – xiaoyi Nov 14 '12 at 16:27
  • I was thinking maybe there is some way that you could return an array that are references to the objects in the original array. But I guess, in retrospect the array is just references anyway to objects so filter will probably work. – user1500053 Nov 14 '12 at 16:28
  • @user1500053 If an array value is an object, a copy of that array will reference the same object. But if the value is a primitive, you'll get a copy of the value. – bfavaretto Nov 14 '12 at 16:32
  • @xiaoyi it's an acronym for "Original Post" - on Stackoverflow, it's a way of referring to the original question that was asked. – Pointy Nov 14 '12 at 16:59
  • @xiaoyi: or **Original Poster**... Depending on the context being used. It's very handy when you can't tell from the user name whether OP is male or female. :) – Robert Koritnik Nov 14 '12 at 17:08
  • thanks RobertKoritnik and Pointy :-) – xiaoyi Nov 14 '12 at 17:10
2

Not bulletproof but it works:

var a = ["a", "b", "c", "d"];
a.splice( a.indexOf("c"), 1 );

console.log(a); // ["a", "b", "d"];
andlrc
  • 41,466
  • 13
  • 82
  • 108