JavaScript遍历数组用splice方法删除元素,这样写可能有遗漏,你遇到过吗?

在编写“圳品”信息系统中,有时需要对二维数组中的数据进行筛选并删除一些元素,比如删除二维数组中首个元素为0的行。

开始是用for循环访问数组+splice方法删除元素来做:

var a = new Array([0,0,0,0],
                  [1,1,1,1],
                  [0,2,2,2],
                  [0,3,3,3],
                  [4,4,4,4]);

var i;

document.write('<p><strong>2、用for循环访问数组删除首个元素值为0的行:</strong></p>');
for (i=0; i < a.length; i++)
{
	if (0==a[i][0])
	{
		document.write('<p>a['+ i +']=' + a[i] + '…… deleted!</p>');
		a.splice(i,1);
	}
}

运行时发现执行结果有时正常,有时会删除不干净,有遗漏。

检查分析后找到了原因:如果二维数组中连续有两行的首个元素为0的行,比如它们分别是第i行和第i+1行,那么用splice方法删除第i行后,第i+1行就会变成第i行,我们应该继续检查第i行,由于for循环每次循环会自动给循环变量i+1,所以下次循环时不再检查第i行,而是检查第i+1行,这样就造成了遗漏。

改进的办法是用while循环:

var a = new Array([0,0,0,0],
                  [1,1,1,1],
                  [0,2,2,2],
                  [0,3,3,3],
                  [4,4,4,4]);
var i;

document.write('<p><strong>2、用while循环访问数组删除首个元素值为0的行:</strong></p>');
i=0;
while(i < a.length)
{
	if (0==a[i][0])
	{
		document.write('<p>a['+ i +']=' + a[i] + '……删除!</p>');
		a.splice(i,1);
	}
	else
	{
		i++;
	}
}   

如果第i行检测到首个元素为0的行则删除,循环变量i不变,进行下一次循环;否则i++。

完整代码如下:

<!DOCTYPE html>
<html>
  <meta name="Author" content="PurpleEndurer">
  <title>“圳品”信息系统</title>
<body>

<script>

var a = new Array([0,0,0,0],
                  [1,1,1,1],
                  [0,2,2,2],
                  [0,3,3,3],
                  [4,4,4,4]);
var i;

document.write('<p style="font-size:16pt; color:purple;"><strong>JavaScript遍历数组用splice方法删除元素,这样写可能有遗漏,你遇到过吗?</strong> @Edge浏览器 by PurpleEndurer</p>');

document.write('<p style="color:blue;"><strong>一、用for循环访问数组+splice方法删除首个元素值为0的行可能有遗漏</strong></p>');
document.write('<p><strong>1、原始数组:</strong></p>');

for (i=0; i < a.length; i++)
{
	document.write((0==a[i][0]) ? ('<p style="color:red;">a['+ i +']=' + a[i] + '</p>')
	                                            : ('<p>a['+ i +']=' + a[i] + '</p>') );
}

document.write('<p><strong>2、用for循环访问数组删除首个元素值为0的行:</strong></p>');
for (i=0; i < a.length; i++)
{
	if (0==a[i][0])
	{
		document.write('<p>a['+ i +']=' + a[i] + '…… deleted!</p>');
		a.splice(i,1);
	}
}

document.write('<p><strong>3、处理后的数组:</strong></p>');
for (i=0; i < a.length; i++)
{
	document.write((0==a[i][0]) ? ('<p style="color:red;">a['+ i +']=' + a[i] + '……应删除但未删除</p>')
	                                            : ('<p>a['+ i +']=' + a[i] + '</p>') );
}

a = new Array([0,0,0,0],
              [1,1,1,1],
              [0,2,2,2],
              [0,3,3,3],
              [4,4,4,4]);

document.write('<p style="color:blue;"><strong>二、用while循环访问数组+splice方法删除首个元素值为0的行</strong></p>');
document.write('<p><strong>1、原始数组:</strong></p>');

for (i=0; i < a.length; i++)
{
	document.write((0==a[i][0]) ? ('<p style="color:red;">a['+ i +']=' + a[i] + '</p>')
	                                            : ('<p>a['+ i +']=' + a[i] + '</p>') );
}

document.write('<p><strong>2、用while循环访问数组删除首个元素值为0的行:</strong></p>');
i=0;
while(i < a.length)
{
	if (0==a[i][0])
	{
		document.write('<p>a['+ i +']=' + a[i] + '……删除!</p>');
		a.splice(i,1);
	}
	else
	{
		i++;
	}
}

document.write('<p><strong>3、处理后的数组:</strong></p>');
for (i=0; i < a.length; i++)
{
	document.write((0==a[i][0]) ? ('<p style="color:red;">a['+ i +']=' + a[i] + '……应删除但未删除</p>')
	                                            : ('<p>a['+ i +']=' + a[i] + '</p>') );
}           
</script>

</body>
</html> 

运行结果截图如下:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫郢剑侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值