標籤: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>
實現:
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/7E/A0/wKiom1cFxhPBSQfBAADzPNdL4HA162.jpg" title="1.jpg" alt="wKiom1cFxhPBSQfBAADzPNdL4HA162.jpg" />
本文出自 “夢想需要堅持” 部落格,請務必保留此出處http://xiyin001.blog.51cto.com/9831864/1761203
javascript Array數組常用方法學習與總結