"for" 循环使我的数据 == 未定义
A "for" loop is making my data == undefined
我正在尝试制作一个在 kahhot 上查找测验的应用程序,但是当我 运行 代码
在我得到这个数据之前,即使是 2 行,数据也变得不确定
- 第一个console.log:卡片:{
类型:'quiz',
标题:'Guess Who',
描述:'Start of term quiz to see how well the class know each other',
塞子:'guess-who',
封面:'https://media.kahoot.it/e6740327-876d-4610-9fba-bc305f4bcae0_opt',
封面元数据:[Object],
draftExists:错误,
number_of_questions: 38,
创建者:'bfdca93b-efba-4ccb-a64d-c5538b24ba94',
creator_username: 's_swan',
creatorPrimaryUsageType: 'SCHOOL',
creator_avatar:{},
徽章:[],
能见度:1,
锁定:假,
写保护:假的,
特色:假的,
young_featured:错误,
赞助:假的,
草稿:假的,
合并:假,
compatibility_level: 6,
sample_questions: [数组],
number_of_plays: 110,
number_of_players: 274,
total_favourites: 0,
question_types: [数组],
创建:1471873436135,
修改:1474121015925,
访问:[Object],
duplication_disabled:错误,
uuid:'b58c19b5-b098-4531-a1c6-e8e735e8f7b2'
}
- 第二个console.log():未定义
在“for”循环中,数据将是未定义的,怎么办?
我有这个代码:
var options = {
'method': 'GET',
'url': 'https://create.kahoot.it/rest/kahoots/?query='+nam+'&cursor=0&limit=20&topics&grades=&orderBy=relevance&searchCluster=1&includeExtendedCounters=False',
'headers': {
'Authorization': 'Bearer '+token
}
};
// Starting the request
request(options, function (error, response, body) {
if (error) throw new Error(error);
var data = JSON.parse(body);
var quizzes = data['entities'];
console.log(quizzes);
// Going the all the quzzies we found
for(quiz in quizzes) {
card = quiz['card'];
console.log(card);
}
});```
第 1 期
根据您的第一个 console.log,看起来您的 quizzes
变量是一个对象....而不是数组,因此您不会遍历测验数组...你正在遍历一个对象...确保你的 quizzes
变量首先是一个数组。
第 2 期
当您使用 for...in
语句时,quiz
变量将 return 对象中每个 属性 的名称。
所以如果我只是 console.log 测验变量,像这样:
for(quiz in quizzes) {
console.log(card);
}
我会看到记录到控制台的所有属性,如下所示:
type
title
description
slug
.....
解决方案:使用for...of
语句
当您使用 for...of
语句时,您设置的变量(quiz
在您的例子中)是整个对象。
所以如果我只是 console.log 测验变量,像这样:
for(const quiz of quizzes) {
console.log(quiz);
}
我会看到这个:
card: {
type: 'quiz',
title: 'Viet Nam War',
description: 'Viet Nam War',
slug: 'viet-nam-war',
draftExists: false,
number_of_questions: 37,
creator: '01f63a9f-737b-4f59-b7f5-af10b78ce904',
creator_username: 'Mr.TBrown',
creatorPrimaryUsageType: 'SCHOOL',
creator_avatar: {},
badges: [],
visibility: 1,
locked: false,
writeProtection: false,
last_edit: [Object],
featured: false,
young_featured: false,
sponsored: false,
moderation: [Object],
draft: false,
combined: false,
compatibility_level: 6,
sample_questions: [Array],
number_of_plays: 65,
number_of_players: 169,
total_favourites: 0,
question_types: [Array],
created: 1527781306997,
modified: 1528072275324,
access: [Object],
duplication_disabled: false,
uuid: 'cea8c609-ee88-4f11-b146-e8c70f302f14'
}
有关更多示例,请参阅以下 MDN 文档:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
如果你想在数组上循环,你通常想使用 for...of
而不是 for...in
并且你还需要添加一个变量声明(const 或 let)。
for(const quiz of quizzes) {
card = quiz['card'];
console.log(card);
}
我正在尝试制作一个在 kahhot 上查找测验的应用程序,但是当我 运行 代码 在我得到这个数据之前,即使是 2 行,数据也变得不确定
- 第一个console.log:卡片:{ 类型:'quiz', 标题:'Guess Who', 描述:'Start of term quiz to see how well the class know each other', 塞子:'guess-who', 封面:'https://media.kahoot.it/e6740327-876d-4610-9fba-bc305f4bcae0_opt', 封面元数据:[Object], draftExists:错误, number_of_questions: 38, 创建者:'bfdca93b-efba-4ccb-a64d-c5538b24ba94', creator_username: 's_swan', creatorPrimaryUsageType: 'SCHOOL', creator_avatar:{}, 徽章:[], 能见度:1, 锁定:假, 写保护:假的, 特色:假的, young_featured:错误, 赞助:假的, 草稿:假的, 合并:假, compatibility_level: 6, sample_questions: [数组], number_of_plays: 110, number_of_players: 274, total_favourites: 0, question_types: [数组], 创建:1471873436135, 修改:1474121015925, 访问:[Object], duplication_disabled:错误, uuid:'b58c19b5-b098-4531-a1c6-e8e735e8f7b2' }
- 第二个console.log():未定义
在“for”循环中,数据将是未定义的,怎么办?
我有这个代码:
var options = {
'method': 'GET',
'url': 'https://create.kahoot.it/rest/kahoots/?query='+nam+'&cursor=0&limit=20&topics&grades=&orderBy=relevance&searchCluster=1&includeExtendedCounters=False',
'headers': {
'Authorization': 'Bearer '+token
}
};
// Starting the request
request(options, function (error, response, body) {
if (error) throw new Error(error);
var data = JSON.parse(body);
var quizzes = data['entities'];
console.log(quizzes);
// Going the all the quzzies we found
for(quiz in quizzes) {
card = quiz['card'];
console.log(card);
}
});```
第 1 期
根据您的第一个 console.log,看起来您的 quizzes
变量是一个对象....而不是数组,因此您不会遍历测验数组...你正在遍历一个对象...确保你的 quizzes
变量首先是一个数组。
第 2 期
当您使用 for...in
语句时,quiz
变量将 return 对象中每个 属性 的名称。
所以如果我只是 console.log 测验变量,像这样:
for(quiz in quizzes) {
console.log(card);
}
我会看到记录到控制台的所有属性,如下所示:
type
title
description
slug
.....
解决方案:使用for...of
语句
当您使用 for...of
语句时,您设置的变量(quiz
在您的例子中)是整个对象。
所以如果我只是 console.log 测验变量,像这样:
for(const quiz of quizzes) {
console.log(quiz);
}
我会看到这个:
card: {
type: 'quiz',
title: 'Viet Nam War',
description: 'Viet Nam War',
slug: 'viet-nam-war',
draftExists: false,
number_of_questions: 37,
creator: '01f63a9f-737b-4f59-b7f5-af10b78ce904',
creator_username: 'Mr.TBrown',
creatorPrimaryUsageType: 'SCHOOL',
creator_avatar: {},
badges: [],
visibility: 1,
locked: false,
writeProtection: false,
last_edit: [Object],
featured: false,
young_featured: false,
sponsored: false,
moderation: [Object],
draft: false,
combined: false,
compatibility_level: 6,
sample_questions: [Array],
number_of_plays: 65,
number_of_players: 169,
total_favourites: 0,
question_types: [Array],
created: 1527781306997,
modified: 1528072275324,
access: [Object],
duplication_disabled: false,
uuid: 'cea8c609-ee88-4f11-b146-e8c70f302f14'
}
有关更多示例,请参阅以下 MDN 文档:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
如果你想在数组上循环,你通常想使用 for...of
而不是 for...in
并且你还需要添加一个变量声明(const 或 let)。
for(const quiz of quizzes) {
card = quiz['card'];
console.log(card);
}