无需预先指定大小、动态的大小,例如:
var names = new Array();
names[0] = "Tom";
names[1] = "Jerry";
names[2] = "Kate";
for(var i = 0; i < names.length; i++) {
alert(names[i]);
}
Array 常用的方法:
array.reverse() 将array中元素的顺序反转;
Array.join("|") 将array数组用|连接成一个字符串
Js中的Array还可以作为Dictionary和Stack使用
例如:
var chitoeng = new Array();
chitoeng["人"] = "person";
chitoeng["学生"] = "student";
chitoeng["国家"] = "country";
chitoeng["中国"] = "china";
// foreach circle to read the key of array chitoeng
// caution the key word 'in' is different from
// java
for(var k in chitoeng) {
alert(k);
}
快速创建创建并立即初始化Array
1. var numberArray = [1, 2, 3, 4, 5, 6];
创建一个类动态数组型的Array
2. var peopleAge = {"tom":30, "jim":35, "jack":27};
创建一个类Map型的javascript的Array容器
例子:foreach循环遍历Array容器,取出workerAge[k],并弹出窗口显示
var workerAge = {"rose":25, "jack":30, "tom":36, "jerry":21 };
for(var k in workerAge) { alert(k + " age is " + workerAge[k]); }