【发布时间】:2017-12-10 18:12:17
【问题描述】:
我正在尝试从 Google 日历中提取选定的数据,以便在客户端使用 ng-repeat 以表单形式显示它。在服务器端,由于缺乏对嵌套的理解,我无法遍历数据
简化的日历 JSON
const calendarList = {
items: [{
summary: 'ABC',
start: [{ datetTime: '2017-12-10T01:40:18.000Z'}],
attendees: [{ displayName: 'John'}]
},
{
summary: 'DEF',
start: [{ datetTime: '2017-12-11T01:40:18.000Z'}],
attendees: [{ displayName: 'Jane'}, { displayName: 'Mary'}]
}]
};
第一层是到对象内部的items,从items,提取summary,start(dateTime) 和与会者(displayName)。有可能多人参加
console.log(calendarList.items[0].summary); //ABC
console.log(calendarList.items[0].start.dateTime); //2017-12-10T01:40:18.000Z
console.log(calendarList.items[0].attendees[0].displayName); //John
首先我将对象转换为数组,从那里我可以访问 items
var calendararray = Object.keys(calendarList)
console.log(calendararray)
// [ 'kind',
// 'etag',
// 'summary',
// 'updated',
// 'timeZone',
// 'accessRole',
// 'defaultReminders',
// 'nextSyncToken',
// 'items' ]
从 items 我写了一个 for 循环,这就是它不起作用的地方。错误消息是Cannot read property 'length' of undefined
for(var i = 0; i < calendararray.items.length; i++)
{
var items = calendarList.items[i];
newGcal.summary += items.summary;
newGcal.datetime += items.start.datetime;
for(var j = 0; j < items.attendees.length; j++)
{
var attendees = items.attendees[j];
newGcal.attendees += attendees.displayName;
}
}
我有 console.log calendararray.items[0] 并且有一个列表,包括 summary、start 和 attendees
console.log(calendararray.items[0])
[ 'kind',
'etag',
'id',
'status',
'htmlLink',
'created',
'updated',
'summary',
'description',
'location',
'creator',
'organizer',
'start',
'end',
'iCalUID',
'sequence',
'attendees',
'guestsCanInviteOthers',
'privateCopy',
'reminders' ]
请提供任何帮助或建议
【问题讨论】:
-
而不是 for(var i = 0; i
-
这不起作用@cdoshi,因为 calendarList 是一个对象,所以它会返回未定义的长度
标签: javascript json nested google-calendar-api