Currently working on an Angular 2 app using Typescript and encountering a challenge. There is a service that retrieves an array of questions structured like this:
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']),
There is a button that enables users to modify the question (including the id). The updateQuestion function appears as follows:
updateQuestion(questionId: number, newQuestion: Question) {
//Find question in array
//Delete this entry
//Add new question to array
}
The main struggle lies in the initial task: locating a question within an array. Attempts have been made using
this.questions.find(question => questionId == id)
however, 'id' is undefined at that point and accomplishing it remains uncertain.
The core issue revolves around pinpointing the correct entry in the questions array where id equals questionId
Any guidance provided will be highly appreciated!!