1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Array类型的操作展示</title> </head> <body>
<script> console.log("Length:") var arr1=[1,2,3,4]; console.log(arr1.length);
arr1.length=2; console.log(arr1); arr1.length=4; console.log(arr1);
console.log("push/pop:") var arr2=[]; arr2.push(1); arr2.push(2,3); console.log(arr2) console.log(arr2[0],arr2[1],arr2[2]);
arr2.pop(); console.log(arr2[0],arr2[1]);
arr2.pop(); arr2.pop(); arr2.pop();
console.log("unshift/shift") arr2=[2,3,4,5]; arr2.unshift(0,1); console.log(arr2);
arr2.shift(); console.log(arr2[0]);
console.log("slice") var arr3=[0,1,2,3,4]; console.log(arr3.slice(2)); console.log(arr3.slice(0,2));
console.log("用slice复制:") var arr4=arr3.slice(); console.log(arr4);
console.log("查找元素:") var arr5=[5,4,3,2,1,0] console.log(arr5.indexOf(3)) console.log(arr5.indexOf(99))
var arr6=[6,5,4,3]; console.log("sort:"); console.log(arr6.sort());
console.log("reverse:") var arr7=[3,4,1]; console.log(arr7.reverse())
console.log("splice:"); var arr8=[4,5,6]; arr8.splice(0,0,"haha"); console.log(arr8[0]);
arr8.splice(0,1,"hehe"); console.log(arr8);
arr8.splice(0,1);
var arr9=[0,1]; var arr10=[2,3]; console.log(arr9.concat(arr10))
var arr11=[1,2,'3']; console.log(arr11.join('-')); </script> </body> </html>
|