Javascript 从javascript数组中删除字符串元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10805874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:01:11  来源:igfitidea点击:

remove string element from javascript array

javascriptjqueryarrays

提问by Mina Gabriel

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

? 提琴手

采纳答案by Hidde

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

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.

你必须自己做一个函数。您可以循环遍历数组并从那里删除元素,或者让此函数为您完成。无论哪种方式,它都不是标准的 JS 功能。

回答by jwatts1980

Since you're using jQuery

由于您使用的是 jQuery

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

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

EDITIf 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);

回答by Selvakumar Arumugam

Try like below,

尝试如下,

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

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

您可以为不支持 indexOf 的浏览器添加以下脚本(来自 MDN

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