/* * js中的map */ function myJsMap() { // 定义myMap,并添加值 var myMap = {}; myMap["key2"] = "value2"; myMap["key1"] = "value1"; myMap["key3"] = "value3"; // 判断某个值是否在myMap中 if ("key2" in myMap) { alert("key2 = " + myMap["key2"]); } // 删除myMap中的值 delete myMap["key2"]; // 遍历myMap for (var key in myMap) { // 调试中myMap的键值对显示为按key排序 // {key1: "value1", key2: "value2", key3: "value3"} // 但是实际输出顺序依然为添加的顺序 // key2=value2,key1=value1,key3=value3 alert(key + " = " + myMap[key]); } }
时间: 2024-10-08 12:33:12