javascript数组方法的使用

本文介绍了JavaScript中数组的基本操作方法,包括如何使用push和unshift方法添加元素,如何使用join连接数组元素,以及如何利用splice进行数组元素的删除与替换。通过具体的代码示例展示了这些方法的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

push方法在数组尾部添加数据,unshift方法在数组头部添加数据。


join使用方法为 a.join("任意字符");

a.reverse(); 

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>

<title>数组方法之增加和删除</title>
<style type="text/css">
	*{margin:0}
	#show{
		width:200px;
		height:200px;
		margin:0 auto;
		color:white;
		background:#2c2c2c;
		text-align:center
	}
</style>
<script type="text/javascript">
	var a = [22,4,111,33];
	/*a.push("<span style='color:red'>a</span>");
	a.pop();
	a.unshift("<span style='color:yellow'>b</span>");*/
	//a.reverse();
	document.write("<div id='show'>");
	document.write(a.concat("www")+"<br/>");
	//document.write(a.join("*")+"<br/>");
	document.write(a);
	
	document.write("</div>");
</script>
</head>
<body>

	

</body>
</html>


代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>

<title>数组方法</title>
<style type="text/css">
	*{margin:0}
	#show{
		width:200px;
		height:200px;
		margin:0 auto;
		color:white;
		background:#2c2c2c;
		text-align:center
	}
</style>
<script type="text/javascript">
	//var a = [33,4,111,22];
	//a.slice(2);
	var a = [1,2,3,4,5,6,7,8,9];
	document.write("<div id='show'>");
	//document.write("结果为:"+a.slice(1,3)+"<br/>");
	document.write("结果为:"+a.splice(4,2,"a")+"<br/>");
	document.write(a.toString());
	document.write("</div>");
</script>
</head>
<body>

	

</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值