JavaScript Array

本文介绍JavaScript中创建和操作数组的多种方法,包括使用构造函数、快捷语法及各种实用的数组方法,如concat、join等,适用于Firefox、Netscape和Internet Explorer等浏览器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

JavaScript provides both a long way and a shortcut to generate and populate an array. The long way is to use the Array object constructor. If you specify no parameters for the constructor, you create an empty array, which you may then populate with data entry by entry:

var myArray = new Array( );
myArray[0] = "Alice";
myArray[1] = "Fred";
...

You do not have to declare a fixed size for an array when you create it, but you may do so if you wish, by passing a single integer value as a parameter to the constructor method:

var myArray = new Array(12);

This creates an array of 12 entries whose values are null.

If you supply more than one comma-delimited parameter to the constructor method, the arguments are treated as data for the array entries. Thus, the following statement:

var myArray = new Array("Alice", "Fred", "Jean");

creates a three-item array, each item containing a string value.

A shortcut approach to the same action lets you use square brackets to symbolize the array constructor:

var myArray = ["Alice", "Fred", "Jean"];

You can use the shortcut syntax in IE 4 or later and NN 4 or later.

 

Array 对象的方法

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

方法描述FFNIE
concat()连接两个或更多的数组,并返回结果。144
join()把数组的所有元素放入一个字符串。元素通过指定的分隔符进行分隔。134
pop()删除并返回数组的最后一个元素。(模拟Stack)145.5
push()向数组的末尾添加一个或更多元素,并返回新的长度。(模拟Stack,Queue)145.5
reverse()颠倒数组中元素的顺序。134
shift()删除并返回数组的第一个元素。(模拟Queue)145.5
slice()从某个已有的数组返回选定的元素144
sort()对数组的元素进行排序。134
splice()删除元素,并向数组添加新元素。145.5
toSource()代表对象的源代码14-
toString()把数组转换为字符串,并返回结果。134
toLocaleString()把数组转换为本地数组,并返回结果。134
unshift()向数组的开头添加一个或更多元素,并返回新的长度。146
valueOf()返回数组对象的原始值。124

Array 对象的属性

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

属性描述FFNIE
constructor对创建此对象的函数的一个引用124
index 134
input 134
length设置或返回数组中元素的数目。124
prototype使您有能力向对象添加属性和方法124
 
 
Note: 利用Array对象可模拟Stack和Queue行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值