-1

Possible Duplicate:
Delete an array key when it contains a string in javascript

by using jquery, underscore or native javaScript I would like to delete or add element to an array.

here is my code;

var a = ['4', '5'];
var remove = false/true; 

if(!remove) {
     a.push('6'); // it works
} else {
     a.remove(5); // I have no idea how to perform this in a very dry way
}
Community
  • 1
  • 1
Lorraine Bernard
  • 13,000
  • 23
  • 82
  • 134

5 Answers5

2

Native JavaScript:

a.splice(1, 1); // ['5']
a // ['4']

You can replace the value with new elements:

var a = [1, 2, 3];
a.splice(1, 1, -1, -2) // [2]
a // [1, -1, -2, 3]

See the MDN documentation

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
2

If you combine NULL and dystroy's answer you can get Niko's answer(ish) in pure js:

a.splice(a.indexOf('5'),1);

Or if you wanted to remove multiple '5's

var p;
while( (p = a.indexOf('5')) != -1 ){
    a.splice(p, 1);
}

A neater method, that is rather unoptimal, only modern-browser supported (and also creates a new array) - but is still valid and more flexible is:

a = a.filter(function(v){
  return v != '5';
});
Pebbl
  • 34,937
  • 6
  • 62
  • 64
0

Use

delete a[1];

or

delete a[a.indexOf('5')];

or

a.splice(a.indexOf('5'), 1);

if you don't want to have a undefined in your array.

For compatibility with IE8, you may want to add a common patch for indexOf

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

Underscore:

a = _.without(a, '5');

See http://underscorejs.org/#without

Niko
  • 26,516
  • 9
  • 93
  • 110
  • 1
    This doesn't remove the element from the array : this builds a new array and leaves the original array untouched. – Denys Séguret Oct 26 '12 at 08:50
  • that will remove all values of '5' not just one – Rune FS Oct 26 '12 at 08:50
  • 1
    By the question it looks like the OP is requesting the removal of the value rather than the offset however - they do not specify if they wish to remove all values however. – Pebbl Oct 26 '12 at 08:51
  • @Pebbl how do you interpret "The best way to add/remove **an** element from an array?" to meen anything than removing an(/one) element? – Rune FS Oct 26 '12 at 08:54
  • @RuneFS because I'm reading what they put below the title. The JS they submitted doesn't actually make sense - it could be read in two ways, hence not being 100% sure of what they require. Plus the question has since been edited and (I admit I haven't looked at the revision) but quite often the title is the first thing to be "rearranged". – Pebbl Oct 26 '12 at 08:59
0

Use splice() method:

if(typeof Array.prototype.remove === "undefined") {
    Array.prototype.remove = function(e) {
        this.splice(this.indexOf(e), 1);
    }
}

var a = ['4', '5'];
a.remove('5');
alert(a);​

Prints:

4

Demo

sp00m
  • 47,968
  • 31
  • 142
  • 252
  • I know this is a john resig kind of thing but there is no reason the extend the prototype chain... – Andreas Louv Oct 26 '12 at 08:53
  • yep.. especially without using `Object.defineProperty` to stop it being enumerable otherwise you'll break a lot of `for(i)` in older code. – Pebbl Oct 26 '12 at 08:55
  • @NULL And why not? It's more convenient to use `a.remove(e)` whereas `a.splice(a.indexOf(e), 1)`, right? – sp00m Oct 26 '12 at 08:55
  • It's a longer talk you can find many articles on google. eg: http://perfectionkills.com/extending-built-in-native-objects-evil-or-not/ – Andreas Louv Oct 26 '12 at 08:58