为什么我的数组值未定义? (JavaScript)的

时间:2016-10-25 06:46:02

标签: javascript arrays

我正在尝试console.log我的" images"中的一个项目。阵列。这是我到目前为止编写的代码:



var projects = {
    projects : [
        {
            "title" : "Jarvis Systems",
            "dates" : "2015",
            "description" : "A simple control panel for stuff.",
            "images" : ["http://www.bitrebels.com/wp-content/uploads/2013/10/MAKO-Voice-Recognition-System-3.jpg", "https://i.ytimg.com/vi/ah6tHb7eWBY/maxresdefault.jpg"]
        }
    ]
};

console.log(projects.projects.images[0])




可悲的是,当我运行代码时,控制台说我的数组是未定义的。当我刚定义时,怎么会这样呢?

2 个答案:

答案 0 :(得分:2)

将其设为projects.projects[0]而不是projects.projects,因为projectsprojects内的数组



var projects = {
    projects : [
        {
            "title" : "Jarvis Systems",
            "dates" : "2015",
            "description" : "A simple control panel for stuff.",
            "images" : ["http://www.bitrebels.com/wp-content/uploads/2013/10/MAKO-Voice-Recognition-System-3.jpg", "https://i.ytimg.com/vi/ah6tHb7eWBY/maxresdefault.jpg"]
        }
    ]
};

console.log(projects.projects[0].images[0])




答案 1 :(得分:1)

项目中有一个数组。

projects.projects[0].images[0]
//               ^^^



var projects = {
    projects : [
        {
            "title" : "Jarvis Systems",
            "dates" : "2015",
            "description" : "A simple control panel for stuff.",
            "images" : ["http://www.bitrebels.com/wp-content/uploads/2013/10/MAKO-Voice-Recognition-System-3.jpg", "https://i.ytimg.com/vi/ah6tHb7eWBY/maxresdefault.jpg"]
        }
    ]
};

console.log(projects.projects[0].images[0])