Я объявил массив как public answers: Answers[] = [];
Здесь Ответы - это мой класс, как показано ниже:
export class Answers {
question : string;
answer : string;
}
Теперь я добавляю значения в answers
, как показано ниже:
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);
});
}
Я получаю ошибку: => ОШИБКА: Аргумент типа строка не может быть назначен параметру типа ответы в this.answers.push(JSON.stringify(data));
.
Я пытался this.answers.push(data);
.После этого я получаю значения в массиве как: Do you Have Multiple locations?,yes,How many Physical locations?,23,Corporate billing info,coporate
Из-за этого всякий раз, когда я пытаюсь обновить значение массива, он обновляется с 0 (нулевым) индексом this.answers
.
let indexValue = this.answers.findIndex(obj => obj.question == data.question);
console.log("Index Value =>" + indexValue);
this.answers[indexValue] = data;
Тем не менее, я получаю значение индекса как -1 (иногда).Может кто-нибудь предложить мне, как хранить значения в массиве, чтобы я мог обновить эти значения после нахождения индекса на основе вопроса (класса модели).