JavaScript数组简单操作方法

简单的关于JS当中数组的一些操作

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	<script>
		function a(){
			var a = new Array("c","c++","java","c#","php");
			var b =[5656456,252,452452];
			var c =["1412","456",'3546'];
			document.writeln("<pre><h3>");
			document.writeln(a);
			a.push("python","go");
			document.writeln(a);
			document.writeln("通过push()方法对数组的最后一个元素来进行删除");
			document.writeln("删除的数组最后一个元素是:"+a.pop());
			document.writeln("删除后的数组为:"+a);
			document.writeln("通过unshift(新数组成员)方法来进行数组的增加"); 
			a.unshift("SQL","c", "myamnesia")
			document.writeln("添加新的数组成员为:"+a[0]+","+a[1]+","+a[2]); 
			document.writeln("添加新元素后的数组元素:" +a);
			document.writeln("通过unshift()方法来删除数组的第一个元素");
			document.writeln("删除的第一个数组元素为:" + a.shift());
			document.writeln("删除后的数组元素为:"+a);
			document.writeln("通过reverse()方法对数组进行反向排序");
			document.writeln("排列后的数组元素为:"+a.reverse());
			document.writeln("通过indexOf返回数组中第一次出现的索引,开始位置的索引为 0。如果在数组中没找到指定元素则返回 -1。");
			document.writeln(a.indexOf("c"));
			document.writeln("通过lastIndexOf返回数组中最后一次出现的索引,开始位置的索引为 0。如果在数组中没找到指定元素则返回 -1。");
			document.writeln(a.lastIndexOf("c"));
			document.writeln("通过Slice(开始位置start,结束位置end)方法从数组中选择(截取)元素,start|end->int,不影响原数组长度");
			document.writeln("截取到的数组为"+a.slice(2,6));
			document.writeln("通过concat()方法进行数组合并:")
			document.writeln("第二个数组为:"+b);
			document.writeln("将数组a,b进行合并 a.concat(b),结果为:"+a.concat(b));
			document.writeln("第二个数组为:"+c);
			document.writeln("将数组a,b,c进行合并 a.concat(b,c),结果为:"+a.concat(b,c)+'<br />');
			document.writeln('但是现在的数组a为:'+a);
			document.writeln('通过sort()让数组进行排序:'+'<br />')
			document.writeln("将数组a按字母顺序进行排序[a.sort()],结果为:"+a.sort());
			document.writeln("将数组b按数字从大到小顺序进行排序[b.sort(function(a,b){return b-a}]);,结果为:"+b.sort(function(a,b){return b-a}));
			document.writeln("将数组c按数字从小到大顺序进行排序[c.sort(function(a,b){return a-b}]);,结果为:"+c.sort(function(a,b){return a-b}));
			document.writeln("将数组b的类型转化为字符串[b.toString()],转换前的数据类型为:"+typeof(b)+"转换后结果为:"+typeof(b.toString()));
			document.writeln("</h3></pre>");
		}
	</script>
	</head>
	<body onload="a()">
	</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值