JavaScript中的Array对象就是数组,首先是一个动态数组,无需预先制定大小,而且是一个像Java中数组、ArrayList、Hashtable等的超强综合体。

一、数组的声明

  常规方式声明:

    1、var arrName = new Array();//创建一个数组

    2、var arrName = new Array([size]); //创建一个数组并指定长度,注意不是上限,是长度

    3、var arrName =new Array("孤傲苍狼","白虎神皇","灭世魔尊");//创建一个数组,并初始化数组的内容

  注意:虽然var arrName = new Array([size]);指定了长度,但实际上所有情况下数组都是变长的,也就是说即使指定了长度为2,仍然可以将元素存储在规定长度以外的,注意:这时长度会随之改变。

  Array的简化声明

    1、普通数组初始化:var arr = [3, 5, 6, 8, 9];

范例1:

 1 <script type="text/javascript">
2 //JavaScript声明数组的四种方式
3 var arr1 = new Array();//创建一个空数组
4 arr1[0]="xdp";
5 arr1[1]="gacl";
6 var arr2 = new Array(2);//创建一个数组并指定长度为2
7 arr2["name0"]="xdp";//arr2第一个元素
8 arr2["name1"]="gacl";//arr2第二个元素
9 arr2["name2"]="xtxd";//arr2第三个元素,arr2虽然在声明时指明了长度为2,但是还是可以添加超过其指明长度的元素
10 var arr3 = new Array("孤傲苍狼","白虎神皇","灭世魔尊");//创建一个数组并初始化数组中的元素
11 var arr4 = [1,true,"String"];//Array的简化声明
12
13 document.write("遍历arr1中的元素:<br/>");
14 for(var i in arr1) {
15 document.write(arr1[i]+"<br/>");
16 }
17 document.write("-----------------------------------------------------------------------------<br/>");
18 document.write("遍历arr2中的元素:<br/>");
19 for(var i in arr2) {
20 document.write("arr2[\""+i+"\"]="+arr2[i]+"<br/>");
21 }
22 document.write("-----------------------------------------------------------------------------<br/>");
23 document.write("遍历arr3中的元素:<br/>");
24 for(var i in arr3) {
25 document.write(arr3[i]+"<br/>");
26 }
27 document.write("-----------------------------------------------------------------------------<br/>");
28 document.write("遍历arr4中的元素:<br/>");
29 for(var i in arr4) {
30 document.write(arr4[i]+"<br/>");
31 }
32 </script>

运行结果:

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnYAAAFK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SqWU3jnJixDar7Z3CxGmKUoBfkL3d6fFve/d+xO7Tutn5Bcv7OoN6hi+b9cQnmDnN9+cDcFOFdxQHWZJsIofjc4cYFpA5hUNzXk8heA8B+CaDiI3/3c9PvnwOBl1pvP/9YNyYxvEWxGozzpM1JE8keHOGewNgWZdF7DvN4utgLzhP6WJfXw4zPii+WWoQ+Q8RC3B/g0h4Ljf/3cxvLjdH8gk3HE9OyK89mvH5o5yyIQB3Mf4u9tHKg3d61vjlVREbNJS9PRw9zdeRuDl+tN5kAO4lipO5nqyZx70LoZkLp8P8G6bj8Fzi+pubTzS1vaMEJxXUDMDixvcQDYMwkxYFlR+Tj9jM2jjL95G/Wd7vYYW3CIBz/con+1IZtlQxABTxWQ8AJUQsAJQQsQBQQsQCQAkRCwAl5n+VBQAIRI8WErEAME0XCwAlRCwAlBCxAFBCxAJACRELACVELACUELEAUELEAkCJwaMnPIACAOaITwAoIWIBoISIBYASIhYASohYACghYgGghIgFgBIiFgBKiFgAKOEBigBQQsQCQIkDzyh+fblf3mzb3FXheQDAYg53sZvQbc4cjgPA15uJ2OHgvvHVyALwa8oj9o3aAODGRCwAlDg/Yptf1gbbAsBXGmRe847i5reqvXuMfRELwG8SewBQQsQCQAkRCwAlRCwAlBCxAFBCxAJACRELACW6EXvWs4X9RiwAv6n7x+yCl8cOIGIB+EkzESsyAWAoFbGH1gIAj/i72Obg/gva/dOJm6s2O4nXeqwxAHc36FZ7QRtPHl5Y3uy5uaF8BeDWxjHWDMjMtPweRCwA3ycVY5nbnU6J2Efn6jEA3M5pdxSf3sUCwK2tFbEPf8IdgG/Rjdg46pq3DSdvDw5eZrIcAG5hrQwTsQB8jeUyzIViAL6DGAOAEiIWAEr8DwaSF3uCC8/uAAAAAElFTkSuQmCC" alt="" />

范例2:

 1 <script type="text/javascript">
2 var names = new Array();//普通方式声明数组,不需要指明数组的长度
3 names[0] = "孤傲苍狼";
4 names[1] = "白虎神皇";
5 names[2] = "灭世魔尊";
6 for (var i = 0; i < names.length; i++) {
7 document.write("names["+i+"] = "+names[i]);
8 document.write("<br/>");
9 }
10
11 var pinyins = new Array();
12 pinyins["人"] = "ren";
13 pinyins["口"] = "kou";
14 pinyins["手"] = "shou";
15 document.write("pinyins[\"人\"] = "+pinyins["人"]);
16 document.write("<br/>");
17 document.write("pinyins.手 = "+pinyins.手);//像Hashtable、Dictionary那样用,而且像它们一样效率高。
18 document.write("<br/>");
19 //Array的简化声明
20 var arr1 = [3, 5];//普通数组初始化
21 for (var i = 0; i < arr1.length; i++) {
22 document.write("arr1["+i+"] = "+arr1[i]);
23 document.write("<br/>");
24 }
25 </script>

运行结果:

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKsAAACCCAIAAAC2Ka+wAAAIUElEQVR4nO1aSZLkIAz0/z/tuUzUMEhKpcRibMhDhw3alSxVXdd9sDeupwM4eBiHAbvjMGB3HAaMwnVd1YP6iqfwCG8ZxZnQmYOrQC/J9kgqL8D1b1D2ybLg4qdSPUgvgexyahOQSGkQA6SLsiWW00rSlVengPGSB4Cjfka86GQwyZMqvVAtPtBRsDpdBjArG8t8mQHqBohVRsTDM0BuAO5KdRdx5XcjBriLaQIDrL+VpLVGMYNLXXf173gKdGFAul6lJGBANWX5snaOii4gNsCVPgwoHchQ1JRAZa1yg064iTVmHoJM5zYYUEViBckHr/IAB9CHAdK3lZUcrx5ceeY1IdAF6lLryICS/dYqUldU+VfVYhPEyePXyh9ggBofDnQRBtxap2UXrUjUrlhxkhnJ8kpJvhRJBqjPgAHAhUXbQQxIr5g0Ayx17IIZscIYvge44zwDGHdpgb5QS9+3JqC7IKRctf8ZcR3I18ZsXTvqK9YFg72As1P3ebBTMvG7axp44eHvz/hV3VHLBykvXVTjOBlVGKt0gWyhzLoSriRvURAyfmYxYMsoL150MgA50pLpSG5RaHWkCglIYn4zawO0PFSNdRmwFIbuLgm4ZOIDXiuxg/k4DNgdhwFhJE4E5iqT9tV4Qq3LgHVugtJdQsUNMnect6e8NAMmqMxxk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6cTp/oBwCnlLvjMGB3HAbsjj8sqrD+LeRrCQAAAABJRU5ErkJggg==" alt="" />

二、数组练习

  Ferris写过一个数组的案例,以下就是他的案例代码,挺全的,思路也挺好!

  1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml"><head>
3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4 <title>数组练习:各种数组方法的使用</title>
5 <style>
6 div{color:green;padding:10px 15px;margin:12px 0;background:#f0f0f0;border:1px dotted #333;font:12px/1.5 Courier New;word-wrap:break-word;}
7 </style>
8 <script type="text/javascript">
9 window.onload = function ()
10 {
11 var aDiv = document.getElementsByTagName("div");
12 var aInput = document.getElementsByTagName("input");
13 var i = 0;
14 var bS1 = bS2 = true;
15 var aTmp = [];
16
17 //删除/添加第一项
18 aInput[0].onclick = function ()
19 {
20 aTmp = getArray(aDiv[0].innerHTML);
21 bS1 ?
22 //删除第一项, shift()方法
23 (aTmp.shift(), this.value = this.value.replace("删除","添加"), bS1 = false) :
24 //添加第一项, unshift()方法
25 (aTmp.unshift("January(1)"), this.value = this.value.replace("添加","删除"), bS1 = true);
26 //输出
27 aDiv[0].innerHTML = aTmp.join()
28 };
29
30
31 //删除/添加最后一项
32 aInput[1].onclick = function ()
33 {
34 aTmp = getArray(aDiv[0].innerHTML);
35 bS2 ?
36 //删除最后一项, pop()方法
37 (aTmp.pop(), this.value = this.value.replace("删除","添加"), bS2 = false) :
38 //添加最后一项, push()方法
39 (aTmp.push("December(12)"), this.value = this.value.replace("添加","删除"), bS2 = true);
40 //输出
41 aDiv[0].innerHTML = aTmp.join()
42 };
43
44
45 //复制, concat()方法
46 aInput[2].onclick = function ()
47 {
48 aTmp = getArray(aDiv[1].innerHTML);
49 //输出
50 aDiv[1].innerHTML = aTmp.concat(aTmp).toString().replace(/\s/g,"")
51 };
52
53
54 //还原, 利用数组的 length 特点
55 aInput[3].onclick = function ()
56 {
57 aTmp = getArray(aDiv[1].innerHTML);
58 //设置数组长度
59 aTmp.length = 10;
60 //输出
61 aDiv[1].innerHTML = aTmp.join()
62 };
63
64
65 //第三组数据还原
66 aInput[4].onclick = function ()
67 {
68 aTmp = ["red","green","blue","white","yellow","black","brown"];
69 //输出
70 aDiv[2].innerHTML = aTmp.join()
71 };
72
73
74 //删除前三项
75 aInput[5].onclick = function ()
76 {
77 aTmp = getArray(aDiv[2].innerHTML);
78 //删除, 0开始, 删除3个
79 aTmp.splice(0, 3);
80 //输出
81 aDiv[2].innerHTML = aTmp.join()
82 };
83
84
85 //删除第二至三项
86 aInput[6].onclick = function ()
87 {
88 aTmp = getArray(aDiv[2].innerHTML);
89 //删除, 2开始, 删除2个
90 aTmp.splice(1, 2);
91 //输出
92 aDiv[2].innerHTML = aTmp.join()
93 };
94
95
96 //在第二顶后插入"orange", "purple"
97 aInput[7].onclick = function ()
98 {
99 aTmp = getArray(aDiv[2].innerHTML);
100 //插入, 2开始, 插入"orange", "purple"
101 aTmp.splice(1, 0, "orange", "purple");
102 //输出
103 aDiv[2].innerHTML = aTmp.join()
104 };
105
106
107 //替换第二项和第三项
108 aInput[8].onclick = function ()
109 {
110 aTmp = getArray(aDiv[2].innerHTML);
111 //插入, 2开始替换
112 aTmp.splice(1, 2, "#009900", "#0000ff");
113 //输出
114 aDiv[2].innerHTML = aTmp.join()
115 };
116
117 //将div中的内容转为数组
118 //str div对象
119 function getArray(str)
120 {
121 aTmp.length = 0;
122 str = str.split(",");
123 for (var i in str)aTmp.push(str[i]);
124 return aTmp
125 }
126 }
127 </script>
128 </head>
129 <body>
130 <div>January(1),February(2),March(3),April(4),May(5),June(6),July(7),Aguest(8),September(9),October(10),November(11),December(12)</div>
131 <input value="删除January(1)" type="button">
132 <input value="删除December(12)" type="button">
133 <div>0,1,2,3,4,5,6,7,8,9</div>
134 <input value="复制" type="button">
135 <input value="还原" type="button">
136 <div>red,green,blue,white,yellow,black,brown</div>
137 <input value="还原" type="button">
138 <input value="删除前三项" type="button">
139 <input value="删除第二至三项" type="button">
140 <input value="在第二项后插入(orange, purple)" type="button">
141 <input value="替换第二项和第三项" type="button">
142
143
144 </body></html>

JavaScript学习总结(八)——JavaScript数组的更多相关文章

  1. Javascript学习4 - 对象和数组

    原文:Javascript学习4 - 对象和数组 在Javascript中,对象和数组是两种基本的数据类型,而且它们也是最重要的两种数据类型. 对象是已命名的值的一个集合,而数组是一种特殊对象,它就像 ...

  2. JavaScript学习(1)之JavaScript基础

    JavaScript学习(1)之JavaScript基础 由于工作原因,开发语言逐渐以JavaScript为主,所以,抽空学习了下JavaScript语法.等现阶段的工作稳定之后,陆续会分享下自己在学 ...

  3. Go语言学习笔记八: 数组

    Go语言学习笔记八: 数组 数组地球人都知道.所以只说说Go语言的特殊(奇葩)写法. 我一直在想一个人参与了两种语言的设计,但是最后两种语言的语法差异这么大.这是自己否定自己么,为什么不与之前统一一下 ...

  4. 转载——JavaScript学习笔记:取数组中最大值和最小值

    转载自:http://www.w3cplus.com/javascript/calculate-the-max-min-value-from-an-array.html. 取数组中最大值 可以先把思路 ...

  5. Javascript学习笔记1 javascript的特点

    ..对于网页而言,Javascript无处不在,对于英语不好的人它简直是噩梦般的存在,但形式所逼,今天开始着手学习!希望自己能坚持下去.从什么地方着手,我的目标是从大处着眼,从应用着眼,不抠细节,反正 ...

  6. JavaScript学习系列3 -- JavaScript arguments对象学习

    在实际项目开发中,目前还是很少使用到JavaScript 中的arguments对象,那么它到底是干什么用的呢 arguments是JavaScript中的一个类数组对象,它代表传给一个正在执行的函数 ...

  7. Javascript学习笔记3 Javascript与BOM简介

    什么是BOM BOM是browser object model的缩写,简称浏览器对象模型 BOM提供了独立于内容而与浏览器窗口进行交互的对象 由于BOM主要用于管理窗口与窗口之间的通讯,因此其核心对象 ...

  8. JavaScript学习笔记(4)——JavaScript语法之变量

    一.变量可以使用短名称(比如 x 和 y),也可以使用描述性更好的名称(比如 age, sum, totalvolume). 变量必须以字母开头 变量也能以 $ 和 _ 符号开头(不过我们不推荐这么做 ...

  9. JavaScript学习笔记:检测数组方法

    检查数组的方法 很多时候我们需要对JavaScript中数据类型(Function.String.Number.Undefined.Boolean和Object)做判断.在JavaScript中提供了 ...

随机推荐

  1. Python 技术点

    1.文件操作 1-1 遍历文件夹和文件 import os rootDir = "/path/to/root" for parent, dirnames, filenames in ...

  2. 05:ModelForm 数据验证 & 生成html & 数据库操作

    目录:Django其他篇 01:Django基础篇 02:Django进阶篇 03:Django数据库操作--->Model 04: Form 验证用户数据 & 生成html 05:Mo ...

  3. 20145220韩旭飞《网络对抗》实验九:web安全基础实践

    基础问答 (1)SQL注入攻击原理,如何防御 原理:把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串. 防御: 利用输入规则限制进行防御,不允许特殊字符输入 (2)XSS攻击的原理,如 ...

  4. 20145322 《网络对抗》 MSF基础应用1

    20145322何志威 Exp5 MS08_067漏洞测试 实验问答 什么是exploit.payload.encode exploit:通过一个漏洞对程序进行攻击的过程 payload:有具体功能作 ...

  5. 20145337《网络对抗技术》逆向及BOF基础

    20145337<网络对抗技术>逆向及BOF基础 实践目标 操作可执行文件pwn1,通过学习两种方法,使main函数直接执行getshall,越过foo函数. 实践内容 手工修改可执行文件 ...

  6. 20145339 Exp5 MS11_050

    20145339 Exp5 MS11_050 实验过程 使用命令msfconsole命令进入控制台 使用命令search ms11_050查看针对MS11_050漏洞的攻击模块 确定相应模块名之后,我 ...

  7. noip 邮票面值设计 - 搜索 - 动态规划

    描述 给定一个信封,最多只允许粘贴N张邮票,计算在给定M(N+M<=10)种邮票的情况下(假定所有的邮票数量都足够),如何设计邮票的面值,能得到最大max ,使得1-max之间的每一个邮资值都能 ...

  8. dp暑假专题 训练记录

    A 回文串的最小划分 题意:给出长度不超过1000的字符串,把它分割成若干个回文字串,求能分成的最少字串数. #include <iostream> #include <cstdio ...

  9. fread

    快速读入? 不知道 反正只是贴过来的 以后或许会用到? #define FI(n) FastIO::read(n) namespace FastIO { << ; ]; int bi = ...

  10. Java初始化块的作用

    1.使代码更简洁 eg: public class Test { private void init() { System.out.println("初始化状态"); } publ ...