如何打印这个javascript对象?

时间:2012-12-04 02:04:03

标签: javascript jquery

我有以下对象数据:

var js_images = {
"newborn_set01_01.jpg":{"filename":"newborn_set01_01.jpg","title":"First Pic","description":"First description..."},
"newborn_set01_02.jpg":{"filename":"newborn_set01_02.jpg","title":"Second Pic","description":"Second description"},
"newborn_set01_03.jpg":{"filename":"newborn_set01_03.jpg","title":"Third Pic","description":"Third description"}
};

我如何做一些简单的事情,比如创建一个警告来显示newborn_set01_03.jpg的描述?

这显然不起作用:

alert(js_images.newborn_set01_03.jpg.description);

1 个答案:

答案 0 :(得分:4)

除了点表示法,您还可以使用括号表示法访问对象成员:

alert(js_images["newborn_set01_03.jpg"].description);

文档:MDN - Member Operators