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