从JavaScript数组中删除字符串元素 [英] remove string element from javascript array
本文介绍了从JavaScript数组中删除字符串元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以有一个人告诉我,我怎样才能从数组中删除字符串元素
我有谷歌这一点,我得到的是由索引号删除
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
我的例子:
var myarray = ["xyz" , "abc" , "def"] ;
var removeMe = "abc" ;
myarray.remove(removeMe) ;
consle.log(myarray) ;
这是我从控制台得到:
Uncaught TypeError: Object xyz,abc,def has no method 'remove'
推荐答案
http://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')
您必须自己做一个功能。您可以循环阵列上,并从那里删除元素,或有此功能为您代劳。无论哪种方式,它不是一个标准的JS特征
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.
这篇关于从JavaScript数组中删除字符串元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文