javascript:对象数组循环

标签 javascript arrays function object

我需要循环一个 javascript 对象数组

这是我的对象响应:

{
 "kind": "calendar#events",
 "etag": "\"p3288namrojte20g\"",
 "summary": "pedicura",
 "updated": "2019-05-01T14:25:51.642Z",
 "timeZone": "America/Argentina/Cordoba",
 "accessRole": "owner",
 "defaultReminders": [
  {
   "method": "popup",
   "minutes": 30
  }
 ],
 "nextSyncToken": "CJCLqtvE-uECEJCLqtvE-uECGAU=",
 "items": [
  {
   "kind": "calendar#event",
   "etag": "\"3113441344690000\"",
   "id": "hb6me2h********1dun5rs10",
   "status": "confirmed",
   "htmlLink": "https://www.google.com/calendar/event?eid=aGI2bWUyaGFvaGtqbWI4bXYxZHVuNXJzMTAgZTYyMXJtMD********Zw",
   "created": "2019-05-01T14:12:08.000Z",
   "updated": "2019-05-01T14:24:32.345Z",
   "summary": "Prueba de: Pedicura en sala 2-x",
   "description": "x-prueba de añadir masajes en sala 2 y notificar al manicura y a otra persona, fernanda?",
   "creator": {
    "email": "guit----@gmail.com"
   },
   "organizer": {
    "email": "e---s90vak@group.calendar.google.com",
    "displayName": "pedicura",
    "self": true
   },
   "start": {
    "dateTime": "2019-05-08T21:00:00-03:00",
    "timeZone": "America/Argentina/Buenos_Aires"
   },
   "end": {
    "dateTime": "2019-05-08T23:00:00-03:00",
    "timeZone": "America/Argentina/Buenos_Aires"
   },
   "iCalUID": "hb6me2ha----s10@google.com",
   "sequence": 2,
   "attendees": [
    {
     "email": "aleja----it@gmail.com",
     "responseStatus": "needsAction"
    },
    {
     "email": "j----d@gmail.com",
     "responseStatus": "needsAction"
    }
   ],
   "reminders": {
    "useDefault": true
   }
  },
  {
   "kind": "calendar#event",
   "etag": "\"3113441378884000\"",
   "id": "tv**0nbhkt**47la0k",
   "status": "confirmed",
   "htmlLink": "https://www.google.com/calendar/event?eid=dHZmOTQwbmJoa3Q3cnJvYjJsMW00N2xhMGsgZTYyMXJtM*****",
   "created": "2019-05-01T14:18:53.000Z",
   "updated": "2019-05-01T14:24:49.442Z",
   "summary": "Prueba de: Pedicura en sala 2-x",
   "description": "x-prueba de añadir masajes en sala 2 y notificar al manicura y a otra persona, fernanda?",
   "creator": {
    "email": "guit----@gmail.com"
   },
   "organizer": {
    "email": "e---s90vak@group.calendar.google.com",
    "displayName": "pedicura",
    "self": true
   },
   "start": {
    "dateTime": "2019-05-07T21:00:00-03:00",
    "timeZone": "America/Argentina/Buenos_Aires"
   },
   "end": {
    "dateTime": "2019-05-07T23:00:00-03:00",
    "timeZone": "America/Argentina/Buenos_Aires"
   },
   "iCalUID": "tvf940nbhk----m47la0k@google.com",
   "sequence": 2,
   "attendees": [
    {
     "email": "ale---it@gmail.com",
     "responseStatus": "needsAction"
    },
    {
     "email": "j--d@gmail.com",
     "responseStatus": "needsAction"
    }
   ],
   "reminders": {
    "useDefault": true
   }
  },
  {
   "kind": "calendar#event",
   "etag": "\"3113441503284000\"",
   "id": "tqhje1kd*****0",
   "status": "confirmed",
   "htmlLink": "https://www.google.com/calendar/event?eid=dHFoamUxa2Q0MG51M2Q3cGd2b2xlOGRqODAgZTYyMX*****",
   "created": "2019-05-01T14:25:51.000Z",
   "updated": "2019-05-01T14:25:51.642Z",
   "summary": "Prueba de",
   "description": "x-prueba de añadir masajes en sala 2 y notificar al manicura y a otra persona, fernanda?",
   "creator": {
    "email": "guit----@gmail.com"
   },
   "organizer": {
    "email": "e---s90vak@group.calendar.google.com",
    "displayName": "prueba",
    "self": true
   },
   "start": {
    "dateTime": "2019-04-26T21:00:00-03:00",
    "timeZone": "America/Argentina/Buenos_Aires"
   },
   "end": {
    "dateTime": "2019-04-26T23:00:00-03:00",
    "timeZone": "America/Argentina/Buenos_Aires"
   },
   "iCalUID": "tqh*****7pgvole8dj80@google.com",
   "sequence": 0,
   "attendees": [
    {
     "email": "-it@gmail.com",
     "responseStatus": "needsAction"
    },
    {
     "email": "-aud@gmail.com",
     "responseStatus": "needsAction"
    }
   ],
   "reminders": {
    "useDefault": true
   }
  }
 ]
}

我想做这样的事情:

var dataStr = JSON.stringify(response);
var dataPrs = JSON.parse(dataStr);
var dataList = JSON.stringify(dataPrs.items);
dataStr = JSON.stringify(dataList);
dataPrs = JSON.parse(dataStr);
dataListPrs.forEach( console.log(dataPrs.htmlLink) );

最佳答案

您不需要每次都执行 JSON.stringifyJSON.parse

仅当您要将 Javascript 对象转换为 JSON 字符串时才需要 JSON.stringify

JSON.parse 仅当您要将 JSON 字符串转换为 Javascript 对象并希望使用 key 访问其值时。

所以,您需要的是:

const data = {
 "kind": "calendar#events",
 "etag": "\"p3288namrojte20g\"",
 "summary": "pedicura",
 "updated": "2019-05-01T14:25:51.642Z",
 "timeZone": "America/Argentina/Cordoba",
 "accessRole": "owner",
 "defaultReminders": [
  {
   "method": "popup",
   "minutes": 30
  }
 ],
 "nextSyncToken": "CJCLqtvE-uECEJCLqtvE-uECGAU=",
 "items": [
  {
   "kind": "calendar#event",
   "etag": "\"3113441344690000\"",
   "id": "hb6me2h********1dun5rs10",
   "status": "confirmed",
   "htmlLink": "https://www.google.com/calendar/event?eid=aGI2bWUyaGFvaGtqbWI4bXYxZHVuNXJzMTAgZTYyMXJtMD********Zw",
   "created": "2019-05-01T14:12:08.000Z",
   "updated": "2019-05-01T14:24:32.345Z",
   "summary": "Prueba de: Pedicura en sala 2-x",
   "description": "x-prueba de añadir masajes en sala 2 y notificar al manicura y a otra persona, fernanda?",
   "creator": {
    "email": "guit----@gmail.com"
   },
   "organizer": {
    "email": "e---s90vak@group.calendar.google.com",
    "displayName": "pedicura",
    "self": true
   },
   "start": {
    "dateTime": "2019-05-08T21:00:00-03:00",
    "timeZone": "America/Argentina/Buenos_Aires"
   },
   "end": {
    "dateTime": "2019-05-08T23:00:00-03:00",
    "timeZone": "America/Argentina/Buenos_Aires"
   },
   "iCalUID": "hb6me2ha----s10@google.com",
   "sequence": 2,
   "attendees": [
    {
     "email": "aleja----it@gmail.com",
     "responseStatus": "needsAction"
    },
    {
     "email": "j----d@gmail.com",
     "responseStatus": "needsAction"
    }
   ],
   "reminders": {
    "useDefault": true
   }
  },
  {
   "kind": "calendar#event",
   "etag": "\"3113441378884000\"",
   "id": "tv**0nbhkt**47la0k",
   "status": "confirmed",
   "htmlLink": "https://www.google.com/calendar/event?eid=dHZmOTQwbmJoa3Q3cnJvYjJsMW00N2xhMGsgZTYyMXJtM*****",
   "created": "2019-05-01T14:18:53.000Z",
   "updated": "2019-05-01T14:24:49.442Z",
   "summary": "Prueba de: Pedicura en sala 2-x",
   "description": "x-prueba de añadir masajes en sala 2 y notificar al manicura y a otra persona, fernanda?",
   "creator": {
    "email": "guit----@gmail.com"
   },
   "organizer": {
    "email": "e---s90vak@group.calendar.google.com",
    "displayName": "pedicura",
    "self": true
   },
   "start": {
    "dateTime": "2019-05-07T21:00:00-03:00",
    "timeZone": "America/Argentina/Buenos_Aires"
   },
   "end": {
    "dateTime": "2019-05-07T23:00:00-03:00",
    "timeZone": "America/Argentina/Buenos_Aires"
   },
   "iCalUID": "tvf940nbhk----m47la0k@google.com",
   "sequence": 2,
   "attendees": [
    {
     "email": "ale---it@gmail.com",
     "responseStatus": "needsAction"
    },
    {
     "email": "j--d@gmail.com",
     "responseStatus": "needsAction"
    }
   ],
   "reminders": {
    "useDefault": true
   }
  },
  {
   "kind": "calendar#event",
   "etag": "\"3113441503284000\"",
   "id": "tqhje1kd*****0",
   "status": "confirmed",
   "htmlLink": "https://www.google.com/calendar/event?eid=dHFoamUxa2Q0MG51M2Q3cGd2b2xlOGRqODAgZTYyMX*****",
   "created": "2019-05-01T14:25:51.000Z",
   "updated": "2019-05-01T14:25:51.642Z",
   "summary": "Prueba de",
   "description": "x-prueba de añadir masajes en sala 2 y notificar al manicura y a otra persona, fernanda?",
   "creator": {
    "email": "guit----@gmail.com"
   },
   "organizer": {
    "email": "e---s90vak@group.calendar.google.com",
    "displayName": "prueba",
    "self": true
   },
   "start": {
    "dateTime": "2019-04-26T21:00:00-03:00",
    "timeZone": "America/Argentina/Buenos_Aires"
   },
   "end": {
    "dateTime": "2019-04-26T23:00:00-03:00",
    "timeZone": "America/Argentina/Buenos_Aires"
   },
   "iCalUID": "tqh*****7pgvole8dj80@google.com",
   "sequence": 0,
   "attendees": [
    {
     "email": "-it@gmail.com",
     "responseStatus": "needsAction"
    },
    {
     "email": "-aud@gmail.com",
     "responseStatus": "needsAction"
    }
   ],
   "reminders": {
    "useDefault": true
   }
  }
 ]
}

const items = data.items

items.forEach(x => console.log(x.htmlLink))

关于javascript:对象数组循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55938445/

相关文章:

arrays - 传递给函数时,如何强制警告使用错误大小的数组?

c - 可变长度数组和段错误

javascript - 涉及简单子(monad)字符串的非常奇怪的错误

python - 使用静态方法而不是函数中的子函数的必要性

javascript - History PushState API 不支持 url 中的长片段

javascript - Jquery 允许光标在 document.ready 函数之前进行

Java 泛型 : Cannot create an array of a nested class

javascript - 我需要使用从 Fabric JS 的特定文件夹中获取的字体

javascript - 更改解释脚本标签的默认浏览器行为

javascript - 谁能弄清楚为什么我的 div 上的 addEventListener 不起作用?