Home  >  Article  >  Web Front-end  >  JavaScript中的Array对象使用说明_javascript技巧

JavaScript中的Array对象使用说明_javascript技巧

WBOY
WBOYOriginal
2016-05-16 18:11:471177browse

说它是一个动态数组,是因为动态的添加数据;

复制代码 代码如下:

var myarr = new Array();
myarr[0] = 1;
myarr[1] = 2;
myarr[2] = 3;
myarr[3] = 23;
myarr[4] = 11;

使用for进行遍历;
说它是一个字典对象,是因为他可以以键值的形式进行访问:
复制代码 代码如下:

var dictionary = new Array();
dictionary["谢龙宝"] = "xielongbao";
dictionary["周保翠"] = "zhoubaocui";
dictionary["谢晓月"] = "xiexiaoyue";
alert(dictionary["谢龙宝"]);
alert(dictionary.谢龙宝);
for (var key in dictionary) {
alert("键:"+key+"值:"+dictionary[key]);
}

使用for-in进行遍历,数组是dic的一个特殊情况,数组的键为整数,dic的键为字符串,所以数组同样可以用for-in进行遍历;另外由于js中对象的成员也是以键的形式存在的,所以我们可以使用for-in查看js对象的成员;

js中数组的简化写法:

var arr = [1, 2, 3, 4];
js中dic的简化写法:
var arrdic = { "jim": 30, "tom": 20 };
这一点在与服务器端的交互上很常用,因为现在从服务器端传递过来的数据都是json格式的,即Javascript中的键值对形式方便前端的操作;
说他是Stack是因为它拥有操作stack的pop()、push()等方法;
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn