从嵌套对象获取数据

时间:2016-01-08 17:29:28

标签: javascript

我有一个返回嵌套对象的变量

console.log (moviesTrailer)

返回,

[Object]
  0: Object
    backdrop: "/LvmmDZxkTDqp0DX7mUo621ahdX.jpg"
    cast: null
    created_at: "2016-01-08T17:21:32.112Z"
    crew: null
    id: 35
    image: "/bIuOWTtyFPjsFDevqvF3QrD1aun.jpg"
    movie_id: "10195"
    release_date: "2011-04-28"
    title: "Thor"
    updated_at: "2016-01-08T17:21:32.112Z"
    user_id: null
    __proto__: Objectlength: 1
    __proto__: Array[0]

如何获取ID或标题等数据?

我试过

console.log (moviesTrailer.object[0].id)

console.log (moviesTrailer.data[0].id)

但这会返回TypeError: Cannot read property '0' of undefined

1 个答案:

答案 0 :(得分:1)

moviesTrailer[0].id应该有用。

moviesTrailer是一个对象,其属性名为0,其值为对具有以下属性的对象的引用:

backdrop: "/LvmmDZxkTDqp0DX7mUo621ahdX.jpg"
cast: null
created_at: "2016-01-08T17:21:32.112Z"
crew: null
id: 35
image: "/bIuOWTtyFPjsFDevqvF3QrD1aun.jpg"
movie_id: "10195"
release_date: "2011-04-28"
title: "Thor"
updated_at: "2016-01-08T17:21:32.112Z"
user_id: null
__proto__: Objectlength: 1
__proto__: Array[0]

demo here