如何在angularjs中处理响应中的动态键

时间:2018-03-14 15:33:18

标签: angularjs

我的后端有以下回复。 "2018-03-18T03:30UTC"是动态的,并且会根据时间不断变化。我想在给定的UTC日期内获取信息。从响应中处理动态密钥的任何方法。

 response =   {
      "id": 1252,
      "name": "S1",
      "description": "sss",
      "entity_type": "AGN",
      "media": [

      ],
      "event": [
        {
          "id": 1263,
          "name": "Agenda Debug Event",
          "media": [
            {
              "id": 1262,
              "entity_type": "MED",
              "media_type": "IMG",
              "media_element": "https://s3-us-west-1.amazonaws.com/wizcard-image-bucket-dev/kiArBud7-alconf.jpeg",
              "media_iframe": "",
              "media_sub_type": "BNR"
            }
          ],
          "start": "2018-03-18T03:30:06Z",
          "end": "2018-03-19T13:00:00Z"
        }
      ],
      "entity_state": "PUB",
      "agenda_items": {
        "2018-03-19T03:30UTC": [
          {
            "id": 1261,
            "name": "s14",
            "description": "aseee",
            "start": "2018-03-19T12:29:29Z",
            "end": "2018-03-19T13:29:29Z",
            "venue": "15",
            "speakers": [
              676
            ],
            "media": [

            ],
            "agenda": 1252,
            "num_users": 0
          },
          {
            "id": 1265,
            "name": "s14",
            "description": "aseee",
            "start": "2018-03-19T12:29:29Z",
            "end": "2018-03-19T13:29:29Z",
            "venue": "15",
            "speakers": [
              676
            ],
            "media": [

            ],
            "agenda": 1252,
            "num_users": 0
          },
          {
            "id": 1272,
            "name": "s14",
            "description": "aseee",
            "start": "2018-03-19T12:29:29Z",
            "end": "2018-03-19T13:29:29Z",
            "venue": "15",
            "speakers": [
              676
            ],
            "media": [

            ],
            "agenda": 1252,
            "num_users": 0
          }
        ],
        "2018-03-18T03:30UTC": [
          {
            "id": 1253,
            "name": "seesion1",
            "description": "ascsds",
            "start": "2018-03-18T10:30:38Z",
            "end": "2018-03-18T12:30:38Z",
            "venue": "12",
            "speakers": [
              736
            ],
            "media": [

            ],
            "agenda": 1252,
            "num_users": 0
          },
          {
            "id": 1256,
            "name": "S12",
            "description": "sss",
            "start": "2018-03-18T10:29:29Z",
            "end": "2018-03-18T10:29:29Z",
            "venue": "",
            "speakers": [

            ],
            "media": [

            ],
            "agenda": 1252,
            "num_users": 0
          },
          {
            "id": 1258,
            "name": "s13",
            "description": "asddd",
            "start": "2018-03-18T11:29:29Z",
            "end": "2018-03-18T12:29:29Z",
            "venue": "14",
            "speakers": [
              676
            ],
            "media": [

            ],
            "agenda": 1252,
            "num_users": 0
          },
          {
            "id": 1260,
            "name": "s14",
            "description": "aseee",
            "start": "2018-03-18T12:29:29Z",
            "end": "2018-03-18T13:29:29Z",
            "venue": "15",
            "speakers": [
              676
            ],
            "media": [

            ],
            "agenda": 1252,
            "num_users": 0
          }
        ]
      },
      "agenda_dates": [
        "2018-03-18T03:30UTC",
        "2018-03-19T03:30UTC"
      ]
    }

1 个答案:

答案 0 :(得分:3)

这取决于"处理动态键"。

这并不是特定角度的。如果您只想迭代这些条目,可以使用Object.keys()(MDN Documentation)。

Object.keys(response.agenda_items).forEach(key => {
    // do something here
})

更新

正如Aleksey Solovey所指出的那样,angular.forEach也是可能的。它可能看起来像这样

 angular.forEach(response.agenda_items,(value, key) => { 
    /* do something here */ 
 }

也可以使用其他Object函数,如Object.entries()或Object.values(),具体取决于您的用例