首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用字母替换JSON obj

用字母替换JSON obj
EN

Stack Overflow用户
提问于 2022-03-23 10:03:04
回答 2查看 48关注 0票数 0

此代码的输出将是namechoices,我希望将Not effectiveNeither effective nor EffectiveEffective替换为"A“、"B”和"C“。分别使用。

我不知道我的代码有什么问题或遗漏了什么,似乎它只适用于以下情况:问题1选择Not effective,问题2选择Neither effective nor Effective,问题3选择Effective

代码语言:javascript
复制
   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
        }
    });
EN

回答 2

Stack Overflow用户

发布于 2022-03-23 10:08:17

从您的输出来看,我认为您是在寻找replaceAll而不是replace

你可以在这里看一下,对象/字符串/替换所有

票数 0
EN

Stack Overflow用户

发布于 2022-03-23 14:37:07

.replace()返回一个新字符串(它不修改现有字符串)

因此,您需要替换并分配回选项数组。为了快递。

代码语言:javascript
复制
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");

工作演示:

代码语言:javascript
复制
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;
    });
  }
})
代码语言:javascript
复制
<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,字符串是不可变的--现有字符串从不修改。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71585242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档