当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JS数组,JS Array

JavaScript
JS数学对象参考
JS字符串对象参考
JS函数参考
JS事件参考
Javascript 常用正则表达式
FF和IE下的js兼容性问题
jQuery 简单介绍
jQuery / 核心 / $(expression, [context] ) 函数
jQuery / 核心 / $(html) 函数
如何使用JS来判断浏览器类型(ie、firefox,等等)
Javascript在IE和FireFox中的不同表现
3个js字符编码函数区别
javascript 中的 XMLDOM 对象

JavaScript 中的 JS数组,JS Array


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 855 ::
收藏到网摘: n/a

The Array object is used to store a set of values in a single variable name.
阵列(数组)对象用来在一单独的变量名称内存储一系列值。


Examples
举例

Create an array [创建一个数组]
Create an array, assign values to it, and write the values to the output.
建立一数组,给它分派值,并输出其值。

For...In Statement [For ... In 语句]
How to use a for...in statement to loop through the elements of an array.
使用for..in语句来在一数组元素中循环

Join two arrays - concat() [加入两个数组]
How to use the concat() method to join two arrays.
怎样使用concat()方法来加入两个数组

Put array elements into a string - join() [将数组元素转变为一个字符串]
How to use the join() method to put all the elements of an array into a string.
怎样使用join()方法来将所有的数组元素变为字符串

Literal array - sort() [按字母顺序排列数组]
How to use the sort() method to sort a literal array.
怎样使用sort()方法来排列数组(字母顺序)

Numeric array - sort() [按数字顺序排列数组]
How to use the sort() method to sort a numeric array.
使用sort()方法来排列数组(数字顺序)

Tip: You will find a lot more examples in the Array object reference - look at the bottom of this page!
提示:你将 在Array object reference找到更多举例(页面底部)


Defining Arrays
定义数组

The Array object is used to store a set of values in a single variable name.
数组对象被用来存储值(在单一的变量名称中)

We define an Array object with the new keyword. The following code line defines an Array object called myArray:
我们通过“new”关键字来定义一个数组对象。下面的代码行定义了称为myArry的数组对象:

var myArray=new Array()

There are two ways of adding values to an array (you can add as many values as you need to define as many variables you require).
有两种方法来添加数组值(你可以添加你所需要的值并定义你所想要的变量名称)

1:

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

You could also pass an integer argument to control the array's size:
还可以通过引入一个整数来控制数组的大小:

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

mycars[2]="BMW"

2:

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

Note: If you specify numbers or true/false values inside the array then the type of variables will be numeric or Boolean instead of string.
注意:如果你在数组里指定数字或真/假值那么变量的类型将变为数字型或布尔型替换了字符串型


Accessing Arrays
访问数组

You can refer to a particular element in an array by referring to the name of the array and the index number. The index number starts at 0.
你可以指示数组的名称和索引数字来从数组中提出一个单独的元素。索引数字从0开始。

The following code line:
正如下面的代码行:

document.write(mycars[0])

will result in the following output:
将有下面的输出结果:

Saab

 


Modify Values in Existing Arrays
修改现有的数组值

To modify a value in an existing array, just add a new value to the array with a specified index number:
要修改现有数组的值只需要通过添加指定索引数字里的值

mycars[0]="Opel"

Now, the following code line:
现在,下面的代码行:

document.write(mycars[0])

will result in the following output:
现在,下面的代码行:

Opel

 


Complete Array Object Reference
完整的数组对象参考

For a complete reference of all the properties and methods that can be used with the Array object, go to our complete Array object reference.
Date[日期]对象中的所有属性和方法参数,我们将在 完整数组对象参数 中罗列说明。

The reference contains a brief description and examples of use for each property and method!
我们将列举简要说明和典型案例来讲解每个参数的属性和方法的用法。

评论 (0) All

登陆 | 还没注册?