Javascript 如何创建数组并在Node.js中传递
我想传递获取的数据,如下面的Javascript 如何创建数组并在Node.js中传递,javascript,node.js,express,Javascript,Node.js,Express,我想传递获取的数据,如下面的array { category: ['Animals','Vehicles'] difficulty: ['easy','medium'] } 我尝试了以下内容,getCategory.push(quick.getCategory(2)), 但我遇到了以下错误 意外令牌。 我搞不清楚如何传递类似数组的数据 如果有人有意见,请告诉我 谢谢 const API_KEY="https://opentdb.com/api.php?amount=10
array
{ category: ['Animals','Vehicles']
difficulty: ['easy','medium']
}
我尝试了以下内容,getCategory.push(quick.getCategory(2)),
但我遇到了以下错误
意外令牌。
我搞不清楚如何传递类似数组的数据
如果有人有意见,请告诉我
谢谢
const API_KEY="https://opentdb.com/api.php?amount=10&type=multiple";
const fetch = require('node-fetch');
const Quiz=require("../public/javascripts/quiz");
module.exports={
getQuiz:function(resp){
fetch(API_KEY)
.then(response => response.json())
.then(json => { const quiz = new Quiz(json);
resp.send({
getCategory:quiz.getCategory(1),
getCategory.push(quiz.getCategory(2)),
getDifficulty:quiz.getDifficulty(1),
getDifficulty.push(quiz.getDifficulty(2))
});
});
}
};
我的班在后面
class Quiz {
constructor(quizData){
this._quizzes = quizData.results;
this._correctAnswersNum = 0;
}
getNumOfQuiz(){
return this._quizzes.length;
}
getCategory(index){
return this._quizzes[index-1].category;
}
getDifficulty(index){
return this._quizzes[index-1].difficulty;
}
}
module.exports = Quiz;
results:
[ { category: 'Animals',
type: 'multiple',
difficulty: 'easy',
question: 'What do you call a baby bat?',
correct_answer: 'Pup',
incorrect_answers: [Array] },
{ category: 'Vehicles',
type: 'multiple',
difficulty: 'medium',
question: 'Which supercar company is from Sweden?',
correct_answer: 'Koenigsegg',
incorrect_answers: [Array] },
{ category: 'Entertainment: Board Games',
type: 'multiple',
difficulty: 'hard',
question: 'Which board game was first released on February 6th, 1935?',
correct_answer: 'Monopoly',
incorrect_answers: [Array] }]
fetch(API_键)
返回如下内容
class Quiz {
constructor(quizData){
this._quizzes = quizData.results;
this._correctAnswersNum = 0;
}
getNumOfQuiz(){
return this._quizzes.length;
}
getCategory(index){
return this._quizzes[index-1].category;
}
getDifficulty(index){
return this._quizzes[index-1].difficulty;
}
}
module.exports = Quiz;
results:
[ { category: 'Animals',
type: 'multiple',
difficulty: 'easy',
question: 'What do you call a baby bat?',
correct_answer: 'Pup',
incorrect_answers: [Array] },
{ category: 'Vehicles',
type: 'multiple',
difficulty: 'medium',
question: 'Which supercar company is from Sweden?',
correct_answer: 'Koenigsegg',
incorrect_answers: [Array] },
{ category: 'Entertainment: Board Games',
type: 'multiple',
difficulty: 'hard',
question: 'Which board game was first released on February 6th, 1935?',
correct_answer: 'Monopoly',
incorrect_answers: [Array] }]
我没有看到在这个实现中我们到底需要如何使用类方法。。 但我们可以这样做 这是我们收到的数据
const data = [ { category: 'Animals',
type: 'multiple',
difficulty: 'easy',
question: 'What do you call a baby bat?',
correct_answer: 'Pup',
incorrect_answers: [Array] },
{ category: 'Vehicles',
type: 'multiple',
difficulty: 'medium',
question: 'Which supercar company is from Sweden?',
correct_answer: 'Koenigsegg',
incorrect_answers: [Array] },
{ category: 'Entertainment: Board Games',
type: 'multiple',
difficulty: 'hard',
question: 'Which board game was first released on February 6th, 1935?',
correct_answer: 'Monopoly',
incorrect_answers: [Array] }];
我们要作为响应发送的数据结构
const finalResponse = {
category : [],
difficulty : []
}
在实际数据上循环,并在finalResponse
中收集数据。可能是如果您已经知道数据的索引
,您可以使用问题中提到的if类
。目前,我们已经使用数据
数组原型方法forEach
在每个数据上循环,如下所示
data.forEach(function({category, difficulty}){
finalResponse.category.push(category);
finalResponse.category.push(difficulty);
})
最后将收集的数据作为响应发送
res.send(finalResponse);
我没有看到在这个实现中我们到底需要如何使用类方法。。 但我们可以这样做 这是我们收到的数据
const data = [ { category: 'Animals',
type: 'multiple',
difficulty: 'easy',
question: 'What do you call a baby bat?',
correct_answer: 'Pup',
incorrect_answers: [Array] },
{ category: 'Vehicles',
type: 'multiple',
difficulty: 'medium',
question: 'Which supercar company is from Sweden?',
correct_answer: 'Koenigsegg',
incorrect_answers: [Array] },
{ category: 'Entertainment: Board Games',
type: 'multiple',
difficulty: 'hard',
question: 'Which board game was first released on February 6th, 1935?',
correct_answer: 'Monopoly',
incorrect_answers: [Array] }];
我们要作为响应发送的数据结构
const finalResponse = {
category : [],
difficulty : []
}
在实际数据上循环,并在finalResponse
中收集数据。可能是如果您已经知道数据的索引
,您可以使用问题中提到的if类
。目前,我们已经使用数据
数组原型方法forEach
在每个数据上循环,如下所示
data.forEach(function({category, difficulty}){
finalResponse.category.push(category);
finalResponse.category.push(difficulty);
})
最后将收集的数据作为响应发送
res.send(finalResponse);
回答1(快速修复):
答案2(正确方式):- 课堂测验:介绍获取数组的新方法| getPropArray()//我不擅长命名
- 课堂测验:介绍获取数组的新方法| getPropArray()//我不擅长命名
谢谢由于我不熟悉,我编辑了这个问题。我试图转换输出结果。谢谢。您需要发送数组作为响应吗?谢谢。由于我不熟悉,我编辑了这个问题。我尝试转换输出结果。谢谢您需要将数组作为响应发送吗?