[整理]JavaScript Array(数组)对象详解

数组对象的作用是:使用单独的变量名来存储一系列的值。

Array Object Methods  数组对象方法

FF: Firefox, N: Netscape, IE: Internet Explorer

Method 方法 Description 描述 FF N IE
concat() Joins two or more arrays and returns the result
将两个或两个以上的数组值连接起来,合并后返回结果 1 4 4

join() Puts all the elements of an array into a string. The elements are separated by a specified delimiter
将数组中的所有元素合并起来成为一条字符串。每个元素可以指定他们的分隔标记。 1 3 4

pop() Removes and returns the last element of an array
删除并返回数组最后的元素 1 4 5.5

push() Adds one or more elements to the end of an array and returns the new length
在数组的末尾加上一个或多个元素,并且返回新的数组长度值 1 4 5.5

reverse() Reverses the order of the elements in an array
将数组中的排列顺序做反向排列 1 3 4

shift() Removes and returns the first element of an array
删除并返回数组中第一个元素值 1 4 5.5

slice() Returns selected elements from an existing array
从现有数组中的某个元素开始依次返回 1 4 4

sort() Sorts the elements of an array
对数组中的元素进行排序 1 3 4

splice() Removes and adds new elements to an array
为数组删除并添加新的元素 1 4 5.5

toSource() Represents the source code of an object
显示对象的源代码 1 4 -

toString() Converts an array to a string and returns the result
将数组转换为字符串,并返回结果 1 3 4

unshift() Adds one or more elements to the beginning of an array and returns the new length
为数组的开始部分加上一个或多个元素,并且返回该数组的新长度 1 4 6

valueOf() Returns the primitive value of an Array object
返回数组对象的原始值 1 2 4




Array Object Properties 数组对象属性

Property 属性 Description 描述 FF N IE
constructor A reference to the function that created the object
所建立对象的函数参考 1 2 4

index 1 3 4

input 1 3 4

length Sets or returns the number of elements in an array
设置或返回数组内元素的数字 1 2 4

prototype Allows you to add properties and methods to the object
为对象加上属性和方法 1 2 4



定义数组

数组对象用来在单独的变量名中存储一系列的值。

我们使用关键词 new 来创建数组对象。下面的代码定义了一个名为 myArray 的数组对象:

var myArray=new Array()

有两种向数组赋值的方法(你可以添加任意多的值,就像你可以定义你需要的任意多的变量一样)。
1:

var mycars=new Array()
mycars[0]="Saab"
mycars[1]="Volvo"
mycars[2]="BMW"

也可以使用一个整数自变量来控制数组的容量:

var mycars=new Array(3)
mycars[0]="Saab"
mycars[1]="Volvo"
mycars[2]="BMW"

2:

var mycars=new Array("Saab","Volvo","BMW")

var mun=[1,2,3];

注意:如果你需要在数组内指定数值或者逻辑值,那么变量类型应该是数值变量或者布尔变量,而不是字符变量。
访问数组

通过指定数组名以及索引号码,你可以访问某个特定的元素。

下面是代码行:

document.write(mycars[0])

下面是输出:

Saab




通过for...in可以遍历数组

下面是代码:

var a=[1,2,3];
for(i in a){
document.write(a[i]);
}

输出结果为:123






JavaScript Array常用数组方法汇总

shift:删除原数组第一项,并返回删除元素的值;如果数组为空则返回undefined
var a = [1,2,3,4,5];
var b = a.shift(); //a:[2,3,4,5]        b:1

unshift:将参数添加到原数组开头,并返回数组的长度
var a = [1,2,3,4,5];
var b = a.unshift(-2,-1); //a:[-2,-1,1,2,3,4,5]        b:7
注:在IE6.0下测试返回值总为undefined,FF2.0下测试返回值为7,所以这个方法的返回值不可靠,需要用返回值时可用splice代替本方法来使用。

pop:删除原数组最后一项,并返回删除元素的值;如果数组为空则返回undefined
var a = [1,2,3,4,5];
var b = a.pop(); //a:[1,2,3,4]        b:5

push:将参数添加到原数组末尾,并返回数组的长度
var a = [1,2,3,4,5];
var b = a.push(6,7); //a:[1,2,3,4,5,6,7]        b:7

concat:返回一个新数组,是将参数添加到原数组中构成的
var a = [1,2,3,4,5];
var b = a.concat(6,7); //a:[1,2,3,4,5]        b:[1,2,3,4,5,6,7]

splice(start,deleteCount,val1,val2,...):从start位置开始删除deleteCount项,并从该位置起插入val1,val2,...
var a = [1,2,3,4,5];
var b = a.splice(2,2,7,8,9); //a:[1,2,7,8,9,5]        b:[3,4]
var b = a.splice(0,1); //同shift
a.splice(0,0,-2,-1); var b = a.length; //同unshift
var b = a.splice(a.length-1,1); //同pop
a.splice(a.length,0,6,7); var b = a.length; //同push

reverse:将数组反序
var a = [1,2,3,4,5];
var b = a.reverse(); //a:[5,4,3,2,1]        b:[5,4,3,2,1]

sort(orderfunction):按指定的参数对数组进行排序
var a = [1,2,3,4,5];
var b = a.sort(); //a:[1,2,3,4,5]        b:[1,2,3,4,5]

slice(start,end):返回从原数组中指定开始下标到结束下标之间的项组成的新数组
var a = [1,2,3,4,5];
var b = a.slice(2,5); //a:[1,2,3,4,5]        b:[3,4,5]

join(separator):将数组的元素组起一个字符串,以separator为分隔符,省略的话则用默认用逗号为分隔符
var a = [1,2,3,4,5];
var b = a.join("|"); //a:[1,2,3,4,5]        b:"1|2|3|4|5"

你可能感兴趣的:(JavaScript,IE,prototype,firefox)