I need to loop an javascript object array
This is my object 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": "[email protected]"
},
"organizer": {
"email": "[email protected]",
"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": "[email protected]",
"sequence": 2,
"attendees": [
{
"email": "[email protected]",
"responseStatus": "needsAction"
},
{
"email": "[email protected]",
"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": "[email protected]"
},
"organizer": {
"email": "[email protected]",
"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": "[email protected]",
"sequence": 2,
"attendees": [
{
"email": "[email protected]",
"responseStatus": "needsAction"
},
{
"email": "[email protected]",
"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": "[email protected]"
},
"organizer": {
"email": "[email protected]",
"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*****[email protected]",
"sequence": 0,
"attendees": [
{
"email": "[email protected]",
"responseStatus": "needsAction"
},
{
"email": "[email protected]",
"responseStatus": "needsAction"
}
],
"reminders": {
"useDefault": true
}
}
]
}
I want to loop items (inside of body) and I've tryed something like this:
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) );
and also:
cons items = data.items;
items.forEach(x => console.log(x.htmlLink))
and also:
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));
but items is always undefined
5 Answers 5
Based on what you supplied, I would expect these lines to work:
const items = response.items;
console.log("items", items);
items.forEach(x => console.log(x.htmlLink));
It worked in my test when I set response to the JSON you provided. So I would expect response is not what you think you it is when this code runs.
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":"[email protected]"},"organizer":{"email":"[email protected]","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":"[email protected]","sequence":2,"attendees":[{"email":"[email protected]","responseStatus":"needsAction"},{"email":"[email protected]","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":"[email protected]"},"organizer":{"email":"[email protected]","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":"[email protected]","sequence":2,"attendees":[{"email":"[email protected]","responseStatus":"needsAction"},{"email":"[email protected]","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":"[email protected]"},"organizer":{"email":"[email protected]","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*****[email protected]","sequence":0,"attendees":[{"email":"[email protected]","responseStatus":"needsAction"},{"email":"[email protected]","responseStatus":"needsAction"}],"reminders":{"useDefault":true}}]}
const items = response.items;
console.log("items", items);
items.forEach(x => console.log(x.htmlLink));
5 Comments
response is not what you expect when trying to use it. Can you console.log(response) before trying to use it? Then share the results.result: {…}. After that, there are still a lot of errors. However, we've determined your issue is in the response, not your code. Hopefully, you have control over the response.You can do it with forEach() to get the htmlLinks from the json object like below-
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":"[email protected]"},"organizer":{"email":"[email protected]","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":"[email protected]","sequence":2,"attendees":[{"email":"[email protected]","responseStatus":"needsAction"},{"email":"[email protected]","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":"[email protected]"},"organizer":{"email":"[email protected]","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":"[email protected]","sequence":2,"attendees":[{"email":"[email protected]","responseStatus":"needsAction"},{"email":"[email protected]","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":"[email protected]"},"organizer":{"email":"[email protected]","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*****[email protected]","sequence":0,"attendees":[{"email":"[email protected]","responseStatus":"needsAction"},{"email":"[email protected]","responseStatus":"needsAction"}],"reminders":{"useDefault":true}}]}
obj['items'].forEach(myFunction);
function myFunction(item, index) {
if(item.hasOwnProperty('htmlLink')){
console.log(item.htmlLink)
}
}
6 Comments
JSON.parse() to make it object before further processing.let obj = JSON.parse(response);?If the response is coming from Promise, you may need to loop on response.data.items
Comments
I solved, it was on @Always Sunny and on @Karl Galvez way; helped also the @Sandeep P statement because i was on a .then
thank you very much to all who helped and tyed.
Comments
// this should work
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": "[email protected]"
},
"organizer": {
"email": "[email protected]",
"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": "[email protected]",
"sequence": 2,
"attendees": [
{
"email": "[email protected]",
"responseStatus": "needsAction"
},
{
"email": "[email protected]",
"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": "[email protected]"
},
"organizer": {
"email": "[email protected]",
"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": "[email protected]",
"sequence": 2,
"attendees": [
{
"email": "[email protected]",
"responseStatus": "needsAction"
},
{
"email": "[email protected]",
"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": "[email protected]"
},
"organizer": {
"email": "[email protected]",
"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*****[email protected]",
"sequence": 0,
"attendees": [
{
"email": "[email protected]",
"responseStatus": "needsAction"
},
{
"email": "[email protected]",
"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);
}
}
}
dataentity is never declared anywhere?