Javascript在数组上未定义

时间:2018-11-08 16:46:02

标签: javascript

我具有以下javascript数据结构:

var data =  {
    "announcements": {
        "IFT4S": [{
            "id": "D7214",
            "read_state": "unread",
            "posted_at": "2018-10-25T14:35:54Z",
            "title": "Reminder! Systems disruption: 27-28 Oct",
            "message": "All University online systems will be unavailable."
        }, {
            "id": "B399C",
            "read_state": "read",
            "posted_at": "2018-10-22T09:04:48Z",
            "title": "Stem Fair",
            "message": "The STEM Careers Fair is taking place on 31 October 2018"
        }, {
            "id": "6F5EE",
            "read_state": "unread",
            "posted_at": "2018-10-22T09:04:48Z",
            "title": "Smile more, worry less with our FREE course",
            "message": "Take part in our Online Mindfulness Programme."
        }]
    }
}

我想访问键“ read_state”,“ posted_at”,“ title”和“ message”的值。 但是,当我尝试使用data.announcements.IFT4S["title"]或任何其他键而不是“ title”时,在控制台中会显示 undefined 。 我在做什么错了?

3 个答案:

答案 0 :(得分:1)

您必须遍历数组以从对象数组获取值

var data = {
  "announcements": {
    "IFT4S": [
    {
        "id": "D7214",
        "read_state": "unread",
        "posted_at": "2018-10-25T14:35:54Z",
        "title": "Reminder! Systems disruption: 27-28 Oct",
        "message": "All University online systems will be unavailable."
      },
      {
        "id": "B399C",
        "read_state": "read",
        "posted_at": "2018-10-22T09:04:48Z",
        "title": "Stem Fair",
        "message": "The STEM Careers Fair is taking place on 31 October 2018"
      },
      {
        "id": "6F5EE",
        "read_state": "unread",
        "posted_at": "2018-10-22T09:04:48Z",
        "title": "Smile more, worry less with our FREE course",
        "message": "Take part in our Online Mindfulness Programme."
      },
    ]
  }
}

data.announcements.IFT4S.forEach(item => {
  console.log(item.title)
})

或者您可以这样做, 0是索引

    console.log(data.announcements.IFT4S[0].read_state)
    console.log(data.announcements.IFT4S[0].title)

答案 1 :(得分:1)

  

当我尝试data.announcements.IFT4S [“ title”]或其他任何键时   “标题”的标题我在控制台中未定义。我在做什么错了?

您在这里正在尝试访问IFT4S阵列的 title 键。

问题是IFT4S没有 title 键。而是像数组对象一样,将索引作为键。

IFT4S = [ {...}, {...}, {...} ]

要访问IFT4S数组的第一个元素,您需要这样做

IFT4S[0]

在您的情况下,该对象将在IFT4S数组的第一个位置(索引0 )返回对象

{
    id: "D7214",
    read_state: "unread",
    posted_at: "2018-10-25T14:35:54Z",
    title: "Reminder! Systems disruption: 27-28 Oct",
    message: "All University online systems will be unavailable."
}

如果要从IFT4S数组中的所有元素中获取所有标题,则可以这样做

IFT4S.map(element => element.title)

Array.prototype.map返回一个新数组,其中每个元素都是将map内部指定的函数应用于原始数组的每个元素的结果。

在这种情况下,它将返回

[
    "Reminder! Systems disruption: 27-28 Oct",
    "Stem Fair",
    "Smile more, worry less with our FREE course"
]

答案 2 :(得分:0)

IFT4S是一个数组,您可以通过调用以下内容来访问其对象及其值:

data.announcements.IFT4S[index].title

此处索引为0-2之一,因为该数组包含3个对象。 例如:

data.announcements.IFT4S[0].title

这是一个非常基本的概念,请查看任何javascript指南以了解数组。