Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove string element from javascript array

can some one tell me how can i remove string element from an array i have google this and all i get is removing by index number

my example :

 var myarray = ["xyz" , "abc" , "def"] ; 
 var removeMe = "abc" ; 

  myarray.remove(removeMe) ; 
  consle.log(myarray) ; 

this is what i get from the console :

Uncaught TypeError: Object xyz,abc,def has no method 'remove' 

​ jsfiddle

like image 322
Mina Gabriel Avatar asked May 29 '12 20:05

Mina Gabriel


3 Answers

Since you're using jQuery

myarray.splice($.inArray("abc", myarray), 1);

EDIT If the item isn't in the array, this 'one-liner' will likely throw an error. Something a little better

var index = $.inArray("abc", myarray);
if (index>=0) myarray.splice(index, 1);
like image 118
jwatts1980 Avatar answered Oct 18 '22 18:10

jwatts1980


From https://stackoverflow.com/a/3955096/711129:

Array.prototype.remove= function(){
    var what, a= arguments, L= a.length, ax;
    while(L && this.length){
        what= a[--L];
        while((ax= this.indexOf(what))!= -1){
            this.splice(ax, 1);
        }
    }
    return this;
}
var ary = ['three', 'seven', 'eleven'];

ary.remove('seven')

or, making it a global function:

function removeA(arr){
var what, a= arguments, L= a.length, ax;
while(L> 1 && arr.length){
    what= a[--L];
    while((ax= arr.indexOf(what))!= -1){
        arr.splice(ax, 1);
    }
}
return arr;
}
var ary= ['three','seven','eleven'];
removeA(ary,'seven')

You have to make a function yourself. You can either loop over the array and remove the element from there, or have this function do it for you. Either way, it is not a standard JS feature.

like image 43
Hidde Avatar answered Oct 18 '22 20:10

Hidde


Try like below,

myarray.splice(myarray.indexOf(removeMe),1); 

You can add this below script (from MDN) for browsers that doesn't support indexOf

if (!Array.prototype.indexOf) {  
    Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {  
        "use strict";  
        if (this == null) {  
            throw new TypeError();  
        }  
        var t = Object(this);  
        var len = t.length >>> 0;  
        if (len === 0) {  
            return -1;  
        }  
        var n = 0;  
        if (arguments.length > 0) {  
            n = Number(arguments[1]);  
            if (n != n) { // shortcut for verifying if it's NaN  
                n = 0;  
            } else if (n != 0 && n != Infinity && n != -Infinity) {  
                n = (n > 0 || -1) * Math.floor(Math.abs(n));  
            }  
        }  
        if (n >= len) {  
            return -1;  
        }  
        var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);  
        for (; k < len; k++) {  
            if (k in t && t[k] === searchElement) {  
                return k;  
            }  
        }  
        return -1;  
    }  
}  
like image 24
Selvakumar Arumugam Avatar answered Oct 18 '22 18:10

Selvakumar Arumugam