An array can hold many values under a single name, and you can access the values by referring to an index number.
JS数组可以用一个名字存储许多值,通过index数字引用这些值。
Create an Array
有三种创建数组的方法。
1. Regular:
var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";
2. Condensed: 浓缩的
var myCars=new Array("Saab","Volvo","BMW");
3: Literal: 逐字的
var myCars=["Saab","Volvo","BMW"];
Access an Array
通过index:
var name=myCars[0];
改变数组中某个值:
myCars[0]="Opel";
All JavaScript variables are objects. Array elements are objects. Functions are
objects.
所有JS变量都是对象。数组元素是对象,函数也是对象。
you can have variables of different types in the same Array.因此在同一个数组中,可以有不同类型的变量:objects、functions、arrays
Create New Methods
Prototype is a global constructor in JavaScript. It can construct new properties and methods for any JavaScript Objects.
JS原型是一个全局构造器,它可以为任何JS对象构建新的属性和方法。
Array.prototype.ucase=function()
{
for (i=0;i<this.length;i++)
{this[i]=this[i].toUpperCase();}
}
Array Object Properties
constructor、length、prototype
Array Object Methods
concat(array1,array2,...)joins
two or more arrays, and returns a copy of the joined arrays 不改变existing数组,产生新数组
var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale); //children is: ["Cecilie","Lone","Emil","Tobias","Linus"]
join(separator) joins
all elements of an array into a string 把一个数组中的全部元素连接为一个字符串,separator是可选项,不写则默认使用逗号分隔var fruits = ["Banana", "Orange", "Apple", "Mango"];
var energy = fruits.join(" and "); // Banana and Orange and Apple and Mango
toString() 把数组中元素以逗号分隔开,转换为字符串,效果同join()var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.toString(); // Banana,Orange,Apple,Mango
indexOf(item,
start) search
the array for an element and returns its position, 没找到返回-1,如果item出现多次,只返回第一次出现的position
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Apple"); // 2
lastIndexOf(item,
start) search
the array for an element, starting at the end, and returns its position 从数组末端开始查找,其他同前
var fruits=["Banana","Orange","Apple","Mango","Banana","Orange","Apple"];
var a = fruits.lastIndexOf("Apple"); //6
shift() removes
the first element of an array, and returns that element 删除数组始端元素,返回这个元素值
push() adds
new elements to the end of an array, and returns the new length 在数组末端添加新元素,返回数组新长度
unshift() adds
new elements to the beginning of an array, 在数组始端添加新元素,返回数组新长度
sort(sortfunction) 排序 the
sort order can be either alphabetic or numeric, and either ascending or descending. 默认排序规则是按alphabet和ascending
var points = [40,100,1,5,25,10];
points.sort(function(a,b){return a-b}); // 按数字升序排列:1,5,10,25,40,100
reverse()倒序 reverses
the order of the elements in an array
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
fruits.reverse(); // 按字母降序排列:Orange,Mango,Banana,Apple
slice(start, end) selects the elements starting at the given start argument, and ends at the given end argument但不包含该结束点 and returns the new array, 不写end值则默认一直到数组末端
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1,3); // Orange,Lemon
splice(index,
howmany, item1, item2,...) Adds/Removes elements from an array, howmany指在index位置要删除的item个数, itemX是可选的
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,0,"Lemon","Kiwi"); // Banana,Orange,Lemon,Kiwi,Apple,Mango
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,2); // Banana,Orange
valueOf()
返回数组本身
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var v = fruits.valueOf(); // 数组v的值为["Banana", "Orange", "Apple", "Mango"]