码迷,mamicode.com
首页 > 编程语言 > 详细

javascript Array数组常用方法学习与总结

时间:2016-04-07 19:02:51      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:javascript   array   数组方法   

<script>
		var a=[1,2,3,4];
		document.body.innerHTML=‘<b>‘+a.join()+‘</b>‘+‘</br>‘;
		document.write(‘<b>‘+typeof(a.join(‘ ‘))+‘</b>‘+‘</br>‘);
		document.write(‘<b>‘+a.join(‘ ‘)+‘</b>‘+‘</br>‘);
		document.write(‘<b>‘+a.join(‘-‘)+‘</b>‘+‘</br>‘);			
		document.write(‘<b>‘+a.reverse().join("*")+‘</b>‘+‘<hr>‘);
		var b=["22","555","44","1"];
		document.write(‘<b>‘+b.sort()+‘</b>‘+‘</br>‘);
		var c=["2","555","44","11"];
		document.write(‘<b>‘+c.sort(function(a,b){return a-b})+‘</b>‘+‘</br>‘);		
		var d=["2","5","4","1"];
		document.write(‘<b>‘+d.concat(6,8)+‘</b>‘+‘</br>‘+‘<hr>‘);
		var e=["2","5","4","1"];
		document.write(‘<b>‘+e.splice(1,3)+‘</b>‘+‘</br>‘);
		var f=["2","5","4","1"];
		document.write(‘<b>‘+f.splice(2)+‘</b>‘+‘</br>‘+‘<hr>‘);
		var g=["red","yellow","blue"];
		document.write(‘<b>‘+"修改后数组的新长度为:"+g.push("green","pink")+‘</b>‘+‘</br>‘);
		document.write(‘<b>‘+"删除的数组值为:"+g.pop()+‘</b>‘+‘</br>‘+‘<hr>‘);
		var e=["red","yellow","blue"];
		document.write(‘<b>‘+"删除的数组的一个元素是:"+e.shift()+‘</b>‘+‘</br>‘);
		document.write(‘<b>‘+"数组前端添加元素后,数组的长度:"+e.unshift("red","black")+‘</b>‘+‘</br>‘+‘<hr>‘);
		var f=["red","yellow","blue"];
		document.write(‘<b>‘+"本身的类型为:"+typeof(f)+‘</b>‘+‘</br>‘);
		document.write(‘<b>‘+f.toString()+‘</b>‘+‘</br>‘);
		document.write(‘<b>‘+"使用toString后的类型变为:"+typeof(f.toString())+‘</b>‘+‘</br>‘);
		document.write(‘<b>‘+f.valueOf()+‘</b>‘+‘</br>‘);
		document.write(‘<b>‘+"使用valueOf()后的类型变为:"+typeof(f.valueOf())+‘</b>‘+‘</br>‘+‘<hr>‘);
		var h=["red","yellow","blue","red"];
		document.write(‘<b>‘+"red所在数组的位置是:"+h.indexOf("red")+‘</b>‘+‘</br>‘);
		document.write(‘<b>‘+"从1的位置开始找blue所在数组的位置是:"+h.indexOf("blue",1)+‘</b>‘+‘</br>‘);
		document.write(‘<b>‘+"使用,lastindexOf(),red所在数组的位置是:"+h.lastIndexOf("red")+‘</b>‘+‘</br>‘+‘<hr>‘);
		var data1=[1,2,3,4,5];
		var sum=0;
		data1.forEach(function(value){sum+=value;});
		document.write(‘<b>‘+"sum的值为:"+sum+‘</b>‘+‘</br>‘);
		data1.forEach(function(v,i,data){data[i]=v+3;});
		document.write(‘<b>‘+"data中的值为:"+data1+‘</b>‘+‘</br>‘+‘<hr>‘);
		var data2=[1,2,3,4,5];
		var sum=0;
		data=data2.map(function(x){return sum+=x});
		document.write(‘<b>‘+"原来数组:"+data2+‘</b>‘+‘</br>‘);
		document.write(‘<b>‘+"sum的值:"+sum+‘</b>‘+‘</br>‘);		
		document.write(‘<b>‘+"新数组中的值为:"+data+‘</b>‘+‘</br>‘+‘<hr>‘);
		var data3=[1,2,3,4,5];
		data33=data3.filter(function(x){return x<3;});
		document.write(‘<b>‘+"新数组中的值为:"+data33+‘</b>‘+‘</br>‘);
		data333=data3.filter(function(x){return x%2==0;});
		document.write(‘<b>‘+"新数组中的值为:"+data333+‘</b>‘+‘</br>‘+‘<hr>‘);
        var data4=[1,2,3,4,5];
		data44=data4.every(function(x){return x<3;});
		document.write(‘<b>‘+"使用every()的返回值:"+data44+‘</b>‘+‘</br>‘);
		data444=data4.some(function(x){return  x<3});
		document.write(‘<b>‘+"使用some()的返回值:"+data444+‘</b>‘+‘</br>‘+‘<hr>‘);
		var data5=[1,2,3,4,5];
		var sum=data5.reduce(function(x,y){return x+y},0);
		document.write(‘<b>‘+"sum的值:"+sum+‘</b>‘+‘</br>‘);
		var sum1=data5.reduce(function(x,y,index,data5){return x+y});
		document.write(‘<b>‘+"sum1的值:"+sum1+‘</b>‘+‘</br>‘);
		var sum2=data5.reduce(function(x,y,index,data5){return x*y},2);
		document.write(‘<b>‘+"sum2的值:"+sum2+‘</b>‘+‘</br>‘);

	</script>

实现效果图:

技术分享

本文出自 “梦想需要坚持” 博客,请务必保留此出处http://xiyin001.blog.51cto.com/9831864/1761203

javascript Array数组常用方法学习与总结

标签:javascript   array   数组方法   

原文地址:http://xiyin001.blog.51cto.com/9831864/1761203

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!