12

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

0

4 Answers 4

21

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);
Sign up to request clarification or add additional context in comments.

4 Comments

You don't need to use jQuery for this.
Some browsers don't support indexOf. Jquery's inArray will abstract the funcationality from the browser.
+1 for using >=0 jsperf.com/not-vs-gt-vs-ge/4 (and for correct answer ofc)
far easier if you're using jQuery already, worked like a charm.
9

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.

Comments

5

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;  
    }  
}  

2 Comments

One comment, some browsers don't support indexOf
Also, if indexOf doesn't find the element, you are gonna splice with index -1 which still removes an element
0

more simple solution

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

var theNewArray = myarray.filter(s => s !== removeMe);

console.log(theNewArray); // will return ["xyz" , "def"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.