<script>
var arr1 = new Array(1 ,3 ,5 ,7 , 9);
var arr2 = new Array(2 , 4 , 6 ,8 , 10);
var arr3 = arr1.concat(arr2);
document.write(arr1.length + "," + arr2.length + "," + arr3.length + "<br />");
document.write(arr1 + "," + arr2 + "," + arr3 + "<br />");
var joinStr = arr1.join('##');
document.write(joinStr + "<br />");
var sum = 0 ;
var tmp = null;
while(true) {
tmp = arr1.pop();
if(tmp == undefined) {
break;
}else {
sum+=tmp;
}
}
document.write("sum value : " + sum + '<br />');
document.write('test push method : <br />');
var newarr = new Array();
document.write('length push once:' + newarr.push('hello'));
document.write('length push twice:' + newarr.push('world'));
document.write('length push third:' + newarr.push('java'));
document.write('length push forth:' + newarr.push('javascript'));
document.write('all push over : ' + newarr);
document.write('reverse test : <br />');
var arr5 = new Array();
arr5.push('a');
arr5.push('b');
arr5.push('c');
arr5.push('d');
arr5.push('e');
arr5.push('f');
document.write('print array in the right order :' + arr5 + '<br />');
arr5.reverse();
document.write('arr5 has been reversed : ' + arr5 + '<br />');
document.write('test array shift method<br />');
var arr6 = new Array();
arr6.push('a');
arr6.push('b');
arr6.push('c');
arr6.push('d');
arr6.push('e');
var tt = null;
while(true) {
tt = arr6.shift();
if(tt == undefined) {
break;
} else {
document.write(tt + ',');
}
}
document.write('<br />');
document.write('test slice method : <br />');
var sliceArr = new Array();
sliceArr.push('a');
sliceArr.push('b');
sliceArr.push('c');
sliceArr.push('d');
sliceArr.push('e');
sliceArr.push('f');
document.write('slice with positive index:' + sliceArr.slice(0 , 4) + '<br />');
document.write('slice with one parameter:' + sliceArr.slice(1) + '<br />');
document.write('slice with 1 negative parameter:' + sliceArr.slice(-1) + '<br />');
document.write('slice with 2 negative parameter:' + sliceArr.slice(-3 , -1) + '<br />');
document.write('test sort method , prepare some random numbers<br />');
var ranArr = new Array();
for(var i = 0 ; i < 10 ; i++) {
ranArr.push(parseInt(Math.random() * 1000));
}
document.write('now ranArr :' + ranArr + '<br />');
document.write('after sort(dictionary sort):<br />');
ranArr.sort();
document.write(ranArr);
document.write('after sort (function sort) :<br />');
ranArr.sort(function(a , b) {
return a - b;
});
document.write(ranArr + '<br />');
document.write('test splice method:<br />');
ranArr = new Array(1 ,2 ,3 ,4 ,5 , 6);
var deletedArr = ranArr.splice(2 , 1 , 100 , 101 , 102);
document.write(ranArr + '<br />');
document.write('deleted arr :' + deletedArr + '<br />');
document.write('toString() method:' + new Array(new Date(2000 , 10 ,11) , new Date(2000 , 10 ,12) , new Date(2000 , 10 ,13)) + '<br />');
document.write('toLocaleString() method:' + new Array(new Date(2000 , 10 ,11) , new Date(2000 , 10 ,12) , new Date(2000 , 10 ,13)).toLocaleString() + '<br />');
document.write('test unshift method:<br />');
var arr6 = new Array();
for(var i = 0 ; i < 10 ; i++) {
arr6.unshift(i * 2 + 5);
}
document.write(arr6 + '<br />');
document.write('valueOf() method equals toString() :<br />');
document.write(arr6.valueOf());
</script>
javascript数组复习
最新推荐文章于 2011-05-08 20:13:03 发布