标签:
最近项目JS中需要建一个特殊的颜色库,需要用到类似C#中的dictionary的概念 然后一查发现JS没有dictionary 而是Array
初始化Array
1 colorDic = new Array(); 2 3 colorDiv["耕地"] = "#009900"; 4 colorDiv["园地"] = "#009933"; 5 colorDiv["林地"] = "#66CC33"; 6 colorDiv["草地"] = "#00CC00";
调用
alert(colorDic["耕地"])
遍历
1 var output = ""; 2 for (var key in colordic) { 3 if (output == "") { 4 output = colordic[key]; 5 } 6 else { 7 output += "|" + colordic[key]; 8 } 9 } 10 alert(output);
注意 如果只是单纯的数据 其实也可以进行遍历 只是key变成了1、2、3
1 var dic2 = ["a", "b", "c", "d"]; 2 //遍历字典 3 for (var key in dic2) { 4 //alert(dic2[key]); 5 }
标签:
原文地址:http://www.cnblogs.com/CoffeeEddy/p/5656292.html