Google 日历嵌套 JSON -- 无法读取未定义的 属性 'length'
Google Calendar nested JSON -- Cannot read property 'length' of undefined
我正在尝试从 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、开始(日期时间) 和与会者(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 属性 '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, 开始和与会者
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' ]
请任何帮助或建议
First I convert the object to array, from there I can access items
那是你出错的地方。不要将任何东西转换成任何东西;您可以轻松地直接使用数据。
.forEach()
对此很有帮助,因此您不必编写容易出错的 for
循环。
下面是一个示例,它列出了简化 calendarList
中的所有数据。对于实际数据中的其他相关项目,您应该可以从这里获取它。
我还重新格式化了您的 calendarList
数据,使 object/array 关系更加清晰:
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' }
]
}
]
};
calendarList.items.forEach( function( item ) {
console.log( 'summary:', item.summary );
item.start.forEach( function( dt ) {
console.log( ' start:', dt.datetTime );
});
item.attendees.forEach( function( attendee ) {
console.log( ' attendee:', attendee.displayName );
});
});
我正在尝试从 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、开始(日期时间) 和与会者(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 属性 '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, 开始和与会者
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' ]
请任何帮助或建议
First I convert the object to array, from there I can access items
那是你出错的地方。不要将任何东西转换成任何东西;您可以轻松地直接使用数据。
.forEach()
对此很有帮助,因此您不必编写容易出错的 for
循环。
下面是一个示例,它列出了简化 calendarList
中的所有数据。对于实际数据中的其他相关项目,您应该可以从这里获取它。
我还重新格式化了您的 calendarList
数据,使 object/array 关系更加清晰:
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' }
]
}
]
};
calendarList.items.forEach( function( item ) {
console.log( 'summary:', item.summary );
item.start.forEach( function( dt ) {
console.log( ' start:', dt.datetTime );
});
item.attendees.forEach( function( attendee ) {
console.log( ' attendee:', attendee.displayName );
});
});