In my code, I have defined an array like this: public answers: Answers[] = [];
. The class Answers
looks like this:
export class Answers {
question: string;
answer: string;
}
I am populating the answers
array with values as shown below:
public addAnswersToStack() {
AnswerEmitterService.get('answer').subscribe((data: Answers) => {
var isPresent = this.answers.some((el) => {
return el[0] === data[0];
});
console.log(isPresent);
if (isPresent == true) {
let indexValue = this.answers.findIndex(obj => obj.question == data.question);
console.log("Index Value =>" + indexValue);
this.answers[indexValue] = JSON.stringify(data);
this.uniqueAnswers = Array.from(new Set(this.answers));
} else {
this.answers.push(JSON.stringify(data));
this.uniqueAnswers = Array.from(new Set(this.answers));
}
console.log("Answers=>" + this.answers);
});
}
The error message I am encountering is => ERROR: Argument of type string is not assignable to parameter of type answers in
this.answers.push(JSON.stringify(data));
.
Instead of this.answers.push(data);
, when I try this, the array values become:
Do you Have Multiple locations?,yes,How many Physical locations?,23,Corporate billing info,coporate
Due to this, whenever I attempt to update a value in the array, it gets updated at index 0 of this.answers
.
let indexValue = this.answers.findIndex(obj => obj.question == data.question);
console.log("Index Value =>" + indexValue);
this.answers[indexValue] = data;
However, sometimes I receive an index value of -1. Can someone provide a suggestion on how to store values in an array so that I can update them based on the question (from the model class)?