JS Array

来源:互联网 发布:ubuntu 开机启动脚本 编辑:程序博客网 时间:2024/06/05 18:12

JS Array

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!
我们将列举简要说明和典型案例来讲解每个参数的属性和方法的用法。

相关文章 更多
  • JavaScript基础知识总结
  • JavaScript 参考教程
  • Js 按照MVC模式制作自定义控件[2]
  • javascript语法(1)
  • 在JS中使用RecordSet对象的GetRows方法
  • 一篇不错的介绍JavaScript Window对象的文章
  • Replace 函数
  • Div+CSS+JS树型菜单
  • JScript错误代码大全
  • javascript 转换外部链接样式
  • JS 摘要
  • 一个在时间选择上很有用的代码
  • js+css图片自动等比例缩小且垂直居中
  • Javascript强制设为首页的网页代码
  • 按比例微缩图片的一段小小的JS代码
  • getAttribute() 方法