javascript 数组部分
<html> <body> <script type="text/javascript"> var arr = new Array(6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "James" arr[4] = "Adrew" arr[5] = "Martin" arr.splice(2,1)//从下标为2的开始删除 删除一个元素 document.write(arr + "<br />") arr.splice(3,0,"lisa")//从下标为3的删除 删除0个元素 添加一个元素lisa document.write(arr + "<br />") arr.push("adam")//在数组末尾添加元素adam document.write(arr + "<br />") arr.unshift("russel")//在数组开头添加元素russel document.write(arr + "<br />") </script> </body> </html>
<html> <body> <script type="text/javascript"> var x var mycars = new Array() mycars[0] = "Saab" mycars[1] = "Volvo" mycars[2] = "BMW" document.write("</br>"+mycars.sort());//按照首字母和首个数字的顺序排序 document.write("</br>"+mycars.pop())//删除并显示最后一个 document.write("</br>"+mycars.shift())//删除并显示第一个 document.write("</br>") x=mycars.join(":")//join() 方法用于把数组中的所有元素放入一个字符串。 document.write(x) document.write("</br>"+mycars.reverse())//反向数组中的顺序 document.write("</br>"+mycars.length)//输出数组的长度 </script> </body> </html>
<html> <body> <script type="text/javascript"> function sortNumber(a, b) { return a - b } var arr = new Array(6) arr[0] = "10" arr[1] = "5" arr[2] = "40" arr[3] = "25" arr[4] = "1000" arr[5] = "1" document.write(arr + "<br />") arr.sort() document.write(arr + "<br />") document.write(arr.sort(sortNumber)) </script> </body> </html>
数组中数字排序