Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Vue$set更改嵌套JSON的值?_Json_Vue.js - Fatal编程技术网

如何使用Vue$set更改嵌套JSON的值?

如何使用Vue$set更改嵌套JSON的值?,json,vue.js,Json,Vue.js,我写了下一个循环: [ { "id": 1, "question": "Top Level question", "answers": [ {"id" : 1, "isSelected" : false}, {"id" : 2, "isSelected" : true}, {"id" : 3, "isSelected" : false}, {"id" : 4, "isSelected" : false},

我写了下一个循环:

[
  {
    "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
  }
]
我需要将所有值都更改为true(或者可以将truefalse更改为true,反之亦然)。我不明白如何拿到所需的钥匙。
this.$set('answer.isSelected',true)生成警告,看起来它无法确定应该更改什么键

this.$set('questions.answers.answer.isSelected',true)不生成警告,但我不确定它的更改值是否在正确的位置。
因为我在控制台中看到:

Before:false
之后:false
Before:true
之后:是的


但是我的代码中的所有值都应该设置为true。

如果我理解正确,您不需要使用此。$set()
。您应该能够在每次迭代中直接在当前答案上设置属性:

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);
  }
} 
如果确实需要使用
$set()
(在某些情况下可能有意义),则必须使用keypath中的数组索引:

for(let question of this.questions) {
    for(let answer of question.answers) {
        answer.isSelected = true;
    }
}
this.$set('questions[0].answers[0].isSelected', true);