js中splice方法_js删除数组中指定元素
數(shù)組對(duì)象splice方法:arr.splice(1,1);
這種方式數(shù)組長(zhǎng)度相應(yīng)改變,但是原來的數(shù)組索引也相應(yīng)改變
/*
* 方法:Array.remove(dx)
* 功能:刪除數(shù)組元素.
* 參數(shù):dx刪除元素的下標(biāo).
* 返回:在原數(shù)組上修改數(shù)組
*/
//經(jīng)常用的是通過遍歷,重構(gòu)數(shù)組.
Array.prototype.remove=
function
(dx)
{
if
(isNaN(dx)||dx>
this
.length){
return
false
;}
for
(
var
i=0,n=0;i<
this
.length;i++)
{
if
(
this
[i]!=
this
[dx])
{
this
[n++]=
this
[i]
}
}
this
.length-=1
}
a = [
'1'
,
'2'
,
'3'
,
'4'
,
'5'
];
alert(
"elements: "
+a+
"nLength: "
+a.length);
a.remove(0);
//刪除下標(biāo)為0的元素
alert(
"elements: "
+a+
"nLength: "
+a.length);
原文鏈接:http://www.jb51.net/article/89335.htm
總結(jié)
以上是生活随笔為你收集整理的js中splice方法_js删除数组中指定元素的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 方程自己解(1)——物理信息神经网络(P
- 下一篇: 漫画什么是分布式事务_分布式事务框架(漫