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.
精彩评论