循环一个javascript对象数组(在另一个对象内部)

时间:2019-05-02 00:59:16

标签: javascript arrays object

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

这是我的对象response

{
 "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) );

还有:

cons items = data.items;
items.forEach(x => console.log(x.htmlLink))

还有:

var dataStr = JSON.stringify(response);
var dataPrs = JSON.parse(dataStr);
var body = dataPrs.body.items;
console.log("body", body);
const items = response.items;
console.log("items", items);
items.forEach(x => console.log(x.htmlLink));

但是items始终是undefined

5 个答案:

答案 0 :(得分:1)

您可以使用forEach()来从json对象中获取 htmlLinks ,如下所示-

let obj =  {"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}}]}
    
obj['items'].forEach(myFunction);

function myFunction(item, index) {
  if(item.hasOwnProperty('htmlLink')){
     console.log(item.htmlLink)  
  }
}

答案 1 :(得分:1)

根据您提供的内容,我希望以下几行有效:

const items = response.items;
console.log("items", items);
items.forEach(x => console.log(x.htmlLink));

当我将response设置为您提供的JSON时,它在我的测试中有效。因此,我希望response并非您认为运行此代码时的样子。

const response =  {"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 = response.items;
console.log("items", items);
items.forEach(x => console.log(x.htmlLink));

答案 2 :(得分:1)

如果响应来自Promise,则可能需要在response.data.items上循环

答案 3 :(得分:1)

我解决了,它在@Always Sunny和@Karl Galvez路上;也帮助了@Sandeep P语句,因为我在.then

非常感谢所有帮助和捆绑的人。

答案 4 :(得分:0)

//这应该起作用

var response = {
 "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 items = response.items;

if(items) {
    for (var itemsIter = 0; itemsIter < items.length; itemsIter++) {
        var item = items[itemsIter];
        if(item && item.hasOwnProperty('htmlLink')) {
            console.log(item.htmlLink);
        }
    }
}