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 93 94 95 96 97 98 99 100 101
| <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> </body> </html> <script type="text/javascript"> var a1 = [9, 78, 34,"哈哈", 298, "哈哈", 34]; a1.pop(); a1.push("在末尾新增"); a1.shift(); a1.unshift("在第一个位置新增元素"); a1.splice(2,0, "哈哈", 90); console.log(a1); var a2 = ["哈哈", "喜之螂", "娃娃鱼"]; var newArray = a1.concat(a2); console.log(newArray); var index = a2.indexOf("大鲨鱼"); console.log(index); index = newArray.indexOf("哈哈", 5); console.log(index); index = newArray.lastIndexOf("哈哈"); console.log(index); index = newArray.indexOf("哈哈"); while(index != -1){ console.log("-------------" + index); index = newArray.indexOf("哈哈", index+1); }
var str = newArray.join(); console.log(str); console.log(newArray.toString()); var arr = newArray.slice(2, 6); console.log(arr); var a1 = [12, 16, 111, 222, 234, 26, 21]; a1.sort(function(obj1, obj2){ return obj1 - obj2; }); console.log(a1); var a2 = ["abc", "abb", "bc","l", "f", "gg", "AC","ud", "m", "s"]; for(var s in a2){ console.log(a2[s]); } console.log(a2.sort()); console.log(a2.sort().reverse()); var stu1 = { name:"Lily", age:19 }; var stu2= { name:"Aurora", age:13 }; var stu3 = { name:"Jack", age:24 }; var stu4 = { name:"Rose", age:21 }; var a3 = [stu1, stu2, stu3, stu4]; a3.sort(function(s1, s2){ return s1.age - s2.age; }); console.log(a3); for(var index in a3){ console.log(a3[index]); } </script>
|