首页 > 代码库 > 常见数组操作
常见数组操作
<script type="text/javascript">
var arrayOne=new Array("one","two","three","four","five");
var sortedArray=arrayOne.sort(); //排序
var reverseArray=sortedArray.reverse(); //反序
var arrayTwo=new Array("abc","def","ghi");
var arrayThree=new Array("von","ken","mo");
var joinedArray=arrayOne.concat(arrayTwo,arrayThree); //数组链接
var lineUp=arrayThree.join(‘#‘); //数组转化为字符串
alert(lineUp);
var members=lineUp.split(‘#‘); //字符串转化为数组
alert(members.length);
var sliceOfArray=arrayOne.slice(1,4); //截取字符串
document.write(sliceOfArray[0]+"<br>");
document.write(sliceOfArray[1]+"<br>");
document.write(sliceOfArray[2]+"<br>");
document.write("joinedArray has "+joinedArray.length+" elements<br>");
document.write(joinedArray[0]+"<br>");
document.write(joinedArray[10]+"<br>");
document.write("-----------------------<br>");
document.write(sortedArray[0]+"<br>");
document.write(sortedArray[1]+"<br>");
document.write(sortedArray[2]+"<br>");
document.write(sortedArray[3]+"<br>");
document.write(sortedArray[4]+"<br>");
document.write("--------Reversed----------<br>");
document.write(reverseArray[0]+"<br>");
document.write(reverseArray[1]+"<br>");
document.write(reverseArray[2]+"<br>");
document.write(reverseArray[3]+"<br>");
document.write(reverseArray[4]+"<br>");
</script>
常见数组操作