一:创建一个数组
1: 常规方式:
var myCars=new Array(); myCars[0]="Saab"; myCars[1]="Volvo"; myCars[2]="BMW";
2: 简洁方式:
var myCars=new Array("Saab","Volvo","BMW");
3: 字面:
var myCars=["Saab","Volvo","BMW"];
二:访问数组
通过指定数组名以及索引号码,你可以访问某个特定的元素。
三:实例
/*合并两个数组 - concat()*/ <script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" var arr2 = new Array(3) arr2[0] = "James" arr2[1] = "Adrew" arr2[2] = "Martin" var arr3 = new Array(2) arr3[0] = "William" arr3[1] = "Franklin" document.write(arr.concat(arr2,arr3)) //George,John,Thomas,James,Adrew,Martin,William,Franklin </script>
//用数组的元素组成字符串 - join() <script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr.join(".")) </script>
//pop() 方法用于删除并返回数组的最后一个元素。 <script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr) document.write("<br />") document.write(arr.pop()) document.write("<br />") document.write(arr) //George,John,Thomas //Thomas //George,John </script>
//push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度。 <script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr + "<br />") document.write(arr.push("James") + "<br />") document.write(arr) //George,John,Thomas //4 //George,John,Thomas,James </script>
reverse() 方法用于颠倒数组中元素的顺序。 <script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr + "<br />") document.write(arr.reverse()) //George,John,Thomas //Thomas,John,George </script>
shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。 <script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr + "<br />") document.write(arr.shift() + "<br />") document.write(arr) /* George,John,Thomas George John,Thomas */ </script>
//slice() 方法可从已有的数组中返回选定的元素。 <script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr + "<br />") document.write(arr.slice(1) + "<br />") document.write(arr) /*George,John,Thomas John,Thomas George,John,Thomas*/ </script>
//sort() 方法用于对数组的元素进行排序。 <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" document.write(arr + "<br />") document.write(arr.sort()) /*George,John,Thomas,James,Adrew,Martin Adrew,George,James,John,Martin,Thomas*/ </script>
//splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目。 <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" document.write(arr + "<br />") arr.splice(2,0,"William") document.write(arr + "<br />") /* George,John,Thomas,James,Adrew,Martin George,John,William,Thomas,James,Adrew,Martin*/ </script>
//toString() 方法可把数组转换为字符串,并返回结果。 <script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr.toString()) /* George,John,Thomas*/ </script>
/* valueOf() 方法返回 Array 对象的原始值。 valueOf() 方法通常由 JavaScript 在后台自动调用,并不显式地出现在代码中。 */