从数组中删除指定的值 [英] Remove specified value from array

查看:99
本文介绍了从数组中删除指定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从数组中删除具有指定值的项目?


即数组值1,2,2,5,7,12,15,21

从数组中删除2将返回

1,5,7,12,15,21


(12和21)不删除,重复也被删除)


到目前为止,我有(val是值,ar是数组,返回新数组):


函数removeFromArray(val,ar){

s = String(ar)

//删除第一项(全局搜索)

reRemove = new RegExp("," + val," g")

s = s.replace(reRemove,"")

//如果在数组的开头

reRemove = new RegExp(" ^" + val +",")

s = s.replace(reRemove,"")

//仅删除项目

reRemove = new RegExp(" ^" + val +" $")

s = s.replace(reRemove,"")

返回新数组

}


然而这似乎是返回1,5,7 ,12,151(151是

''的结果,2'被从阵列的'',15''部分移除)


我将如何修改它以使其正常工作(可能是一个

正则表达式 - 虽然分解和解释)?


我也想要它也可以用于字符串(可能包含

逗号)


ie


array =" Bloggs ,Jo"," Doe,John"," Doe,Jane"

删除Doe,Jane"从数组返回Bloggs,Jo,Doe,John

解决方案

")

s = s.replace(reRemove,"")

返回新数组

}


然而这似乎是返回1,5,7,12,151(151是

''的结果,2'被从阵列''15,21''部分移除)


如何修改它以使其正常工作(可能是一个

正则表达式 - 分解并解释)?


我也希望它也能用于字符串(可能包含

逗号)





array =" Bloggs,Jo"," Doe,John"," Doe,Jane"

删除Doe,Jane"从数组返回Bloggs,Jo,Doe,John


Sam Collett写道:

怎么做我从数组中删除了一个具有指定值的项目?

即数组值1,2,2,5,7,12,15,21

从数组中删除2将返回
1,5,7,12,15,21

(12和21不被删除,重复也被删除)

到目前为止我有(val是值,ar是数组,返回新数组):

函数removeFromArray(val,ar){
s = String(ar)
//如果不是先删除item(全局搜索)
reRemove = new RegExp("," + val," g")
s = s.replace(reRemove,"")
//如果在数组开始时删除
reRemove = new RegExp(" ^" + val +",")
s = s.replace(reRemove,"")
//仅删除项目
reRemove = new RegExp(" ^" + val +"

")
s = s.replace(reRemove,"")
返回新数组
}
''的结果,'2'从阵列的'',15''部分被移除)

如何修改它以使其正常工作(可能作为一个正则表达式 - 分解并解释)?

我也希望它能够与之配合使用字符串(可能包含逗号)



array =" Bloggs,Jo",Doe,John",Doe,Jane" ;
删除Doe,Jane从数组返回Bloggs,Jo,Doe,John




首先你说要从数组中删除值,然后你就不要t使用

a JavaScript Array对象。这对我来说没有多大意义。如果您希望从数组中删除项目,则从数组中删除项目:


< script type =" text / javascript">

var theArray = [1,2,2,5,7,12,15,21];


var theNewArray = removeItems(theArray,2) ;

alert(theNewArray.join(","));


函数removeItems(array,item){

var i = 0;

while(i< array.length){

if(array [i] == item){

array.splice(i,1);

} else {

i ++;

}

}

返回数组;

}

< / script>


但是,你对我说,我没有像那样的阵列。没问题,请说

你有逗号分隔的字符串中的项目如原始所示:


var theValues =" 1,2,2,5 ,7,12,15,21" ;;

var theArray = theValues.split(/,/);


为名称执行此操作就像好吧:


var theNewArray =(theNamesArray,Doe,John);


唯一的问题是转动逗号分隔名单列表

一个数组。 ''Doe,John'',''Smith,Joe''"

..split(/,/)无法变成阵列。有很多解决方案,但最简单的是
将使用另一个分隔符(一个不太可能作为一个人的名字出现的一部分

)在原始字符串中):


var theNames ="''Doe,John''@#!''Smith,Joe''@#!''Jones,Mary'' " ;;

var theNamesArray = theNames.split(/ @#!/);


更好的是,将名字和姓氏存储为两个属性一个

对象,然后将该对象存储在数组中。


-

| Grant Wagner< gw ***** @ agricoreunited.com>


*客户端Javascript和Netscape 4 DOM参考可从以下位置获得:

*
http:// devedge .netscape.com / library / ... ce / frames.html


* Internet Explorer DOM参考资料:

*
http://msdn.microsoft.com/研讨会/ a ... ence_entry.asp


* Netscape 6/7 DOM参考资料于:

* http://www.mozilla.org/docs/dom/domref/

*为Netscape 6/7和Mozilla升级JavaScript的提示

* http://www.m. ozilla.org/docs/web-deve...upgrade_2.html


How do I remove an item with a specified value from an array?

i.e. array values 1,2,2,5,7,12,15,21

remove 2 from array would return
1,5,7,12,15,21

(12 and 21 are NOT removed, duplicates are also removed)

So far I have (val is value, ar is array, returns new array):

function removeFromArray(val, ar){
s = String(ar)
// remove if not first item (global search)
reRemove = new RegExp(","+val,"g")
s = s.replace(reRemove,"")
// remove if at start of array
reRemove = new RegExp("^"+val+",")
s = s.replace(reRemove,"")
// remove if only item
reRemove = new RegExp("^"+val+"$")
s = s.replace(reRemove,"")
return new Array(s)
}

However this seems to return 1,5,7,12,151 (the 151 is a result of the
'',2'' being removed from the ''15,21'' part of the array)

How would I modify this to get it to work properly (maybe as one
regular expression - broken down and explained though)?

Also I want it to work with strings as well (possibly containing
commas)

i.e.

array = "Bloggs, Jo", "Doe, John", "Doe, Jane"
remove "Doe, Jane" from array to return "Bloggs, Jo","Doe, John"

解决方案

")
s = s.replace(reRemove,"")
return new Array(s)
}

However this seems to return 1,5,7,12,151 (the 151 is a result of the
'',2'' being removed from the ''15,21'' part of the array)

How would I modify this to get it to work properly (maybe as one
regular expression - broken down and explained though)?

Also I want it to work with strings as well (possibly containing
commas)

i.e.

array = "Bloggs, Jo", "Doe, John", "Doe, Jane"
remove "Doe, Jane" from array to return "Bloggs, Jo","Doe, John"


Sam Collett wrote:

How do I remove an item with a specified value from an array?

i.e. array values 1,2,2,5,7,12,15,21

remove 2 from array would return
1,5,7,12,15,21

(12 and 21 are NOT removed, duplicates are also removed)

So far I have (val is value, ar is array, returns new array):

function removeFromArray(val, ar){
s = String(ar)
// remove if not first item (global search)
reRemove = new RegExp(","+val,"g")
s = s.replace(reRemove,"")
// remove if at start of array
reRemove = new RegExp("^"+val+",")
s = s.replace(reRemove,"")
// remove if only item
reRemove = new RegExp("^"+val+"


")
s = s.replace(reRemove,"")
return new Array(s)
}

However this seems to return 1,5,7,12,151 (the 151 is a result of the
'',2'' being removed from the ''15,21'' part of the array)

How would I modify this to get it to work properly (maybe as one
regular expression - broken down and explained though)?

Also I want it to work with strings as well (possibly containing
commas)

i.e.

array = "Bloggs, Jo", "Doe, John", "Doe, Jane"
remove "Doe, Jane" from array to return "Bloggs, Jo","Doe, John"



First you say you want to remove values from an array, then you don''t use
a JavaScript Array object at all. That doesn''t make much sense to me. If
you want to remove items from an array, then remove items from an array:

<script type="text/javascript">
var theArray = [ 1,2,2,5,7,12,15,21 ];

var theNewArray = removeItems(theArray, 2);
alert(theNewArray.join(", "));

function removeItems(array, item) {
var i = 0;
while (i < array.length) {
if (array[i] == item) {
array.splice(i, 1);
} else {
i++;
}
}
return array;
}
</script>

But, you say to me, "I don''t have an array like that". No problem, say
you have the items in a comma-delimited string as original shown:

var theValues = "1,2,2,5,7,12,15,21";
var theArray = theValues.split(/,/);

Doing it for names would work just as well:

var theNewArray = (theNamesArray, "Doe, John");

The only problem would be turning your comma-delimited list of names into
an array. "''Doe, John'', ''Smith, Joe''" couldn''t be turned into an array by
..split(/,/). There are a number of solutions to this, however the easiest
would be to use another delimiter (one that is unlikely to appear as part
of a person''s name in the original string):

var theNames = "''Doe, John''@#!''Smith, Joe''@#!''Jones, Mary''";
var theNamesArray = theNames.split(/@#!/);

Better still, store the first and last names as two properties of an
object, then store that object in the array.

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html


这篇关于从数组中删除指定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆