在自定义对象数组中查找 属性 的条目
Finding entry by property in custom object array
我正在 Angular 2 中使用 Typescript 开发应用程序,但遇到了问题。我有一项服务可以获取像这样的问题数组
export class Question {
constructor(public id: number,
public question_text: string,
public answers: string[],
public user_answer: string = "none") // default to none, update on user input
{}
}
private questions: Question[] = [
new Question(0,
'How often did you eat a portion of vegetables??',
['Answer A','Answer B', 'Answer C', 'Answer D', 'Answer E', 'Answer F']),
new Question(1,
'How often did you eat a portion of fruit?',
['Answer A','Answer B', 'Answer C', 'Answer D', 'Answer E', 'Answer F']),
然后我有一个按钮,允许用户编辑问题(包括 ID 在内的所有数据)。我的 updateQuestion 看起来像这样:
updateQuestion(questionId: number, newQuestion: Question) {
//Find question in array
//Delete this entry
//Add new question to array
}
我真的很难完成第一个任务:在数组中查找问题。我尝试了
的一些组合
this.questions.find(question => questionId == id)
但那里的 id 未定义,我不确定如何访问它。
问题主要围绕在问题数组中找到正确的条目,其中 id = questionId
任何帮助将不胜感激!!
问题出在问题模型上。我已将问题模型更改为:
export class Question {
public _id: number;
public _question_text: string;
public _answers: string[];
public _user_answer: string;
constructor(public id: number,
public question_text: string,
public answers: string[],
public user_answer: string = "none") // default to none, update on user input
{
this._id = id;
this._question_text = question_text;
this._answers = answers;
this._user_answer = user_answer;
}
}
然后是我的服务:
updateQuestion(questionId: number, newQuestion: Question) {
const questionIndex = this.questions.findIndex(x => x._id == questionId);
我正在 Angular 2 中使用 Typescript 开发应用程序,但遇到了问题。我有一项服务可以获取像这样的问题数组
export class Question {
constructor(public id: number,
public question_text: string,
public answers: string[],
public user_answer: string = "none") // default to none, update on user input
{}
}
private questions: Question[] = [
new Question(0,
'How often did you eat a portion of vegetables??',
['Answer A','Answer B', 'Answer C', 'Answer D', 'Answer E', 'Answer F']),
new Question(1,
'How often did you eat a portion of fruit?',
['Answer A','Answer B', 'Answer C', 'Answer D', 'Answer E', 'Answer F']),
然后我有一个按钮,允许用户编辑问题(包括 ID 在内的所有数据)。我的 updateQuestion 看起来像这样:
updateQuestion(questionId: number, newQuestion: Question) {
//Find question in array
//Delete this entry
//Add new question to array
}
我真的很难完成第一个任务:在数组中查找问题。我尝试了
的一些组合this.questions.find(question => questionId == id)
但那里的 id 未定义,我不确定如何访问它。
问题主要围绕在问题数组中找到正确的条目,其中 id = questionId
任何帮助将不胜感激!!
问题出在问题模型上。我已将问题模型更改为:
export class Question {
public _id: number;
public _question_text: string;
public _answers: string[];
public _user_answer: string;
constructor(public id: number,
public question_text: string,
public answers: string[],
public user_answer: string = "none") // default to none, update on user input
{
this._id = id;
this._question_text = question_text;
this._answers = answers;
this._user_answer = user_answer;
}
}
然后是我的服务:
updateQuestion(questionId: number, newQuestion: Question) {
const questionIndex = this.questions.findIndex(x => x._id == questionId);