【问题标题】:Can't access data. object in another object?无法访问数据。另一个对象中的对象?
【发布时间】:2020-04-27 08:27:18
【问题描述】:

这是有问题的数据

[ 
    { 
        "id":1,
        "date":"2020-01-02",
        "start_time":"11:32:25",
        "end_time":"11:32:26",
        "editions":null,
        "coordinates":null,
        "ranking":null,
        "user_id":3,
        "instructor_id":2,
        "course_id":2,
        "package_id":null,
        "location_id":3,
        "created_at":null,
        "updated_at":null,
        "student_schedules":[ 
            { 
                "id":1,
                "student_schedule_id":1,
                "calification":75,
                "approved":0
            }
        ],
    }
]

我正在尝试在student_schedules 中进行校准。我尝试过以下操作,history 是整个数据

history.student_schedules['calification']

history.student_schedules.calification

这不起作用并给我错误

TypeError:无法读取未定义的属性“校准”

[Vue 警告]:渲染错误:“TypeError: Cannot read property 'calification' of undefined”

我不知道为什么会出现这些错误,数据显然在那里。

如何访问这些数据?

【问题讨论】:

  • 这是一个数组,因此您需要在第一个位置访问它,例如history[0].student_schedules[0].calification;

标签: javascript laravel vue.js


【解决方案1】:

history 是一个对象数组。

因此,您首先需要使用 history[0] 定位数组中的第一个对象

那么,student_schedules 也是一个对象数组,所以你必须在那里做同样的事情,你最终得到:

history[0].student_schedules[0].calification

您可以分别使用{}[] 来区分objectarray

【讨论】:

    【解决方案2】:

    historystudent_schedules 都是可以包含多个对象的数组。如果你想访问一个数组的对象,你需要指定它的索引,例如:

    history[0].student_schedules[0].clarification

    如果数组只包含一个对象,那么您可以删除 []

    { 
        "id":1,
        "date":"2020-01-02",
        "start_time":"11:32:25",
        "end_time":"11:32:26",
        "editions":null,
        "coordinates":null,
        "ranking":null,
        "user_id":3,
        "instructor_id":2,
        "course_id":2,
        "package_id":null,
        "location_id":3,
        "created_at":null,
        "updated_at":null,
        "student_schedules":
            { 
                "id":1,
                "student_schedule_id":1,
                "calification":75,
                "approved":0
            }
    }
    

    然后像这样访问数据:

    history.student_schedules.clarification

    【讨论】:

      猜你喜欢
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 2013-11-18
      相关资源
      最近更新 更多