1. 数组中添加map
var arr = [];
var key = 'Jeremy';
var value = '!!!!'
arr.push({
'key': key,
'value': value,
}); document.write("key: " + arr[]['key'] +
"<br/>value: " + arr[]['value']); 输出结果:
key: Jeremy
value: !!!!
2. 数组遍历输出
var arr = [];
arr.push("Jeremy");
arr.push("Jimmy");
for(var i in arr)
document.write(i + ": " + arr[i] + "</br>"); 输出结果:
: Jeremy
: Jimmy
3. 类似字典(map)遍历
var dict = []; //or dict = new Array()
dict["Jeremy"] = ;
dict["Jimmy"] = ;
for(var key in dict)
document.write(key + ": " + dict[key] + "</br>"); 输出结果:
Jeremy:
Jimmy:
4. 字典声明时赋值
var dict = {
"Jeremy" : ,
"Jimmy" :
};
for(var key in dict)
document.write(key + ": " + dict[key] + "</br>"); 输出结果:
Jeremy:
Jimmy:
demo2
var dict = {
"Jeremy" : ["Chinese", "Math"] ,
"Jimmy" : ["Art", "English"]
};
var name = "Jeremy";
for(var courseIndex in dict[name])
document.write(dict[name][courseIndex] + "</br>"); 输出结果:
Chinese
Math
5. 字典里value为数组, 数组内为字典
var dict = [];
var courseListOfJeremy = [
{"Chinese" : },
{"Math": }
];
dict['Jeremy'] = courseListOfJeremy;
var courseListOfJimmy = [
{"Art": },
{"English": }
];
dict['Jimmy'] = courseListOfJimmy; document.write("Jimmy's Course Number Of Chinese: " + dict['Jeremy'][]['Chinese']); 输出结果:
Jimmy's Course Number Of Chinese: 3