Every time I try to access a deep level property (I guess an item in an array through it's index) I get the following error:
ERROR TypeError: Cannot assign to read only property 'value' of object '[object Object]'
Find below the code I'm trying to get running, but will throw the error above:
@Input() data: CivilLiabilityQuestionnaireModel;
public questions: CivilLiabilityQuestionnaireModel;
this.questions = { ...this.data };
const questionGroupIndex = 0;
const questionGroupItemIndex = 0;
this.questions
.questionGroup[questionGroupIndex]
.questionGroupItems[questionGroupItemIndex]
.answers[0]
.value = form[val];
A little more details of what's possible:
// This works
this.questions.id = 'hotdog';
// This doesn't work
// ERROR TypeError: Cannot assign to read only property 'id' of object '[object Object]'
this.questions.questionGroup[questionGroupIndex].id = 'hamburger';
I thought this would only be possible with using the spread operator, but this will turn all my arrays into objects, while I need to keep my arrays at any cost.
Here the spread solution I tried:
this.questions = {
...this.questions,
questionGroup: {
...this.questions.questionGroup,
[questionGroupIndex]: {
...this.questions.questionGroup[questionGroupIndex],
questionGroupItems: {
...this.questions.questionGroup[questionGroupIndex].questionGroupItems,
[questionGroupItemIndex]: {
...this.questions.questionGroup[questionGroupIndex].questionGroupItems[questionGroupItemIndex],
answers: {
...this.questions.questionGroup[questionGroupIndex].questionGroupItems[questionGroupItemIndex].answers,
[0]: {
...this.questions.questionGroup[questionGroupIndex].questionGroupItems[questionGroupItemIndex].answers[0],
value: form[val]
}
}
}
}
}
}
};