此代码的输出将是name和choices,我希望将Not effective、Neither effective nor Effective、Effective替换为"A“、"B”和"C“。分别使用。
我不知道我的代码有什么问题或遗漏了什么,似乎它只适用于以下情况:问题1选择Not effective,问题2选择Neither effective nor Effective,问题3选择Effective。
Survey
.StylesManager
.applyTheme("defaultV2");
const json = {
pages: [
{
questions: [
{
type: "radiogroup",
name: "Question 1",
title: "Deliver through others.",
choices: [
"Not effective",
"Neither effective nor Effective",
"Effective"
]
},
{
type: "radiogroup",
name: "Question 2",
title: "Understand others perspective.",
choices: [
"Not effective",
"Neither effective nor Effective",
"Effective"
]
},
{
type: "radiogroup",
name: "Question 3",
title: "Solve complex problems.",
choices: [
"Not effective",
"Neither effective nor Effective",
"Effective"
]
},
]
}
]
};
window.survey = new Survey.Model(json);
survey
.onComplete
.add(function (sender) {
let data = JSON.stringify(sender.data)
data = data.replace("Not effective", "A")
data = data.replace("Neither effective nor Effective", "B")
data = data.replace("Effective", "C")
var obj = JSON.parse(data)
document
.querySelector('#surveyResult')
.textContent = "Result JSON:\n" + JSON.stringify(obj, null, 3);
});
var app = new Vue({
el: '#surveyElement',
data: {
survey: survey
}
});发布于 2022-03-23 10:08:17
从您的输出来看,我认为您是在寻找replaceAll而不是replace。
你可以在这里看一下,对象/字符串/替换所有
发布于 2022-03-23 14:37:07
.replace()返回一个新字符串(它不修改现有字符串)
因此,您需要替换并分配回选项数组。为了快递。
choices[0] = choices[0].replace("Not effective", "A");
choices[1] = choices[1].replace("Neither effective nor Effective", "B");
choices[2] = choices[2].replace("Effective", "C");工作演示:
const json = {
pages: [
{
questions: [
{
type: "radiogroup",
name: "Question 1",
title: "Deliver through others.",
choices: [
"Not effective",
"Neither effective nor Effective",
"Effective"
]
},
{
type: "radiogroup",
name: "Question 2",
title: "Understand others perspective.",
choices: [
"Not effective",
"Neither effective nor Effective",
"Effective"
]
},
{
type: "radiogroup",
name: "Question 3",
title: "Solve complex problems.",
choices: [
"Not effective",
"Neither effective nor Effective",
"Effective"
]
},
]
}
]
};
new Vue({
el: '#app',
data: {
questions: json.pages[0].questions,
updatedData: []
},
mounted() {
this.updatedData = this.questions.map((obj) => {
obj.choices[0] = obj.choices[0].replace("Not effective", "A");
obj.choices[1] = obj.choices[1].replace("Neither effective nor Effective", "B");
obj.choices[2] = obj.choices[2].replace("Effective", "C");
return obj;
});
}
})<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div v-for="(question, index) in updatedData" :key="index">
<p v-for="choice in question.choices" :key="choice">
{{ choice}}
</p>
</div>
</div>
FYI In JavaScript,字符串是不可变的--现有字符串从不修改。
https://stackoverflow.com/questions/71585242
复制相似问题