JS 中定义存储key-value


在javascript中,对象本身就是一种Map结构。



var key1 = '动态key1';

var key2 = '动态key2';
var map = {};
map[key1] = 1;
map[key2] = 2;

  

console.log(map[key1]);//结果是1.
console.log(map[key2]);//结果是2.

  

//如果遍历map
for(var prop in map){
    if(map.hasOwnProperty(prop)){
        console.log('key is ' + prop +' and value is' + map[prop]);
    }
}


Last Article

Comment 评论



Share 分享

New Users 最新加入

  • hokurikustr

  • refrain

New comments 最新评论

test123: aasdas Details Apr 13 16:39
admin: Thanks! Details Apr 09 11:46
admin: Google map api Details Apr 09 11:46
lqj12: cooooooooool Details Apr 08 21:34
Yunhan Huang: 这个功能是如何实现的? Details Apr 08 13:23