Vuejs2 如何在Vuejs和Vuex中将无线电输入中的值放入数组
Vuejs2 如何在Vuejs和Vuex中将无线电输入中的值放入数组,vuejs2,radio-button,vuex,vue-cli-3,Vuejs2,Radio Button,Vuex,Vue Cli 3,状态:{ 问题:[ { “id”:1, “名称”:“q1”, “类别”:“英语”, “类型”:“多个”, “问题”:“任何人、动物、地方、事物和感觉的名称是什么?”, “正确答案”:“名词”, “回答不正确”:[ “代词”, “名词”, “副词”, “形容词” ] } ] 答案=”, 答案=[] } 将答案放入数组的逻辑 使用数组索引检查答案是否已输入 如果存在,请删除旧选项并插入最新选项 下面是上述逻辑的代码 // Trying to find index of chosed answer
状态:{
问题:[
{
“id”:1,
“名称”:“q1”,
“类别”:“英语”,
“类型”:“多个”,
“问题”:“任何人、动物、地方、事物和感觉的名称是什么?”,
“正确答案”:“名词”,
“回答不正确”:[
“代词”,
“名词”,
“副词”,
“形容词”
]
}
]
答案=”,
答案=[]
}
将答案放入数组的逻辑
// Trying to find index of chosed answer
const indexOfExistingChoice = this.choosedAnswers.findIndex(
answer => answer.id === selectedAnswer.id
);
if (indexOfExistingChoice >= 0) {
// Choice already selected
// Removing the choice from the array
this.choosedAnswers.splice(indexOfExistingChoice, 1);
}
// Pushing into the array
this.choosedAnswers.push(selectedAnswer);