简体   繁体   English

如何在JavaScript数组中访问对象文字的属性

[英]How to access attributes of object literals in javascript array

Can you tell me what is wrong with this javascript code? 您能告诉我这段JavaScript代码有什么问题吗? It seems like the countries array is not getting initialized correctly, because it doesn't even make it past the initialization to the second document.write("test2"); 似乎countrys数组未正确初始化,因为它甚至没有使它超过第二个document.write(“ test2”);的初始化。 line: 线:

<script>
document.write("test1");
var countries;
countries = [
    {
        name: 'France',
        continent: 'Europe',
        cities: ['Paris', 'Nice'],
        photos: [france1.jpg, france2.jpg]
    },
    {
        name: 'Mexico',
        continent: 'North America',
        cities: ['Tijuana', 'Cancun', 'Mexico City'],
        photos: [mexico1.jpg, mexico2.jpg, mexico3.jpg]
    },
    {
        name: 'China',
        continent: 'Asia',
        cities: ['Beijing', 'Shanghai', 'Hong Kong'],
        photos: [china1.jpg, china2.jpg]
    }
];
document.write("test2");
document.write(countries[0].name);

</script>

The 'photos' need to be quoted. “照片”需要引用。 For example, 例如,

{
    name: 'France',
    continent: 'Europe',
    cities: ['Paris', 'Nice'],
    photos: ['france1.jpg', 'france2.jpg']
}

A quick check of your Javascript error console should have shown you this. 快速检查一下Javascript错误控制台,应该会向您显示此内容。

您必须在photos变量中输入字符串('france1.jpg,')。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM