开发者

how to display the values of a map variable in javascript

开发者 https://www.devze.com 2022-12-29 04:59 出处:网络
I have a javasrcript variable var hash = { \'.bmp\' : 1, \'.gif\' : 1, \'.jpeg\' : 1, \'.jpg\' : 1, \'.png\' : 1,

I have a javasrcript variable

var hash = {
    '.bmp' : 1,
    '.gif' : 1,
    '.jpeg' : 1,
    '.jpg' : 1,
    '.png' : 1,
    '.tif' : 1,
    '.tiff' : 1,    
  };

I want to display the values (.bmp, .gif, .jpeg, .jpg, .png, .tif, .tiff) of this "hash" object in my alert开发者_JS百科 message. How can I do this? Please help.


var text ='',
for(key in hash)
    text += (key + ' = ' + hash[key] + '\n');

alert(text);

Although I must say what you're dealing with here is really an object that has properties that start with a dot, which seems like terribly bad practice to me. If they were without the dot, you could've done hash.bmp for instance, instead of hash['.bmp'].


alert(hash['.bmp']); //alerts 1

You might want to remove the trailing comma after the last item.

0

精彩评论

暂无评论...
验证码 换一张
取 消