remove string element from javascript array

前端 未结 4 1525
我在风中等你
我在风中等你 2020-12-20 15:03

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          


        
相关标签:
4条回答
  • 2020-12-20 15:19

    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;  
        }  
    }  
    
    0 讨论(0)
  • 2020-12-20 15:34

    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.

    0 讨论(0)
  • 2020-12-20 15:41

    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"]
    
    0 讨论(0)
  • 2020-12-20 15:43

    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);
    
    0 讨论(0)
提交回复
热议问题