[
{
"id": 1,
"question": "Top Level question",
"answers": [
{"id" : 1, "isSelected" : false},
{"id" : 2, "isSelected" : true},
{"id" : 3, "isSelected" : false},
{"id" : 4, "isSelected" : false},
{"id" : 5, "isSelected" : false},
{"id" : 6, "isSelected" : false}
],
"MaxAllowedChoice" : 1,
"isEnabled" : true,
"NowSelected" : 0
}
]
我写了下一个循环:
for (let question of this.questions) {
//console.log(question.answers); // array of answers
for(let answer of question.answers) {
//this.$set('answer.isSelected', true);
console.log('Before: ', answer.isSelected);
// this.$set('answer.isSelected', true); //warning: You are setting a non-existent path "answer.isSelected"
this.$set('questions.answers.answer.isSelected', true); // so look like I should to do like this to change value
// this.$set('questions.answers.answer.isSelected', true); //I can't understand how to write it's correctly... like this or above.
console.log('After: ', answer.isSelected);
}
}
我需要将所有值更改为true(或者可以更改true< - > false,反之亦然)。我无法理解如何获得所需的密钥。
this.$set('answer.isSelected', true);
会发出警告,看起来无法找到应该更改的密钥。
this.$set('questions.answers.answer.isSelected', true);
不会产生警告,但我不确定它是否在正确的位置发生变化。
因为我在控制台中看到了:
之前:假 之后:假 之前:真实的 之后:真实
但我的代码中的所有值都应设置为true。
答案 0 :(得分:2)
如果我理解正确,您不需要使用this.$set()
。您应该能够在每次迭代中直接在当前答案上设置属性:
for(let question of this.questions) {
for(let answer of question.answers) {
answer.isSelected = true;
}
}
如果你真的需要使用$set()
(在某些情况下可能有意义),你将不得不在密钥路径中使用数组索引:
this.$set('questions[0].answers[0].isSelected', true);