Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

113
Visualizações
Reemplazando JSON obj con letras

La salida de este código será el name y las choices que quiero reemplazar Not effective , Neither effective nor Effective , Effective con "A", "B" y "C". Respectivamente.

No sé qué está mal o qué falta en mi código, parece que simplemente funciona cuando elijo Not effective para la Pregunta 1, Neither effective nor Effective para la Pregunta 2 y Effective para la Pregunta 3. Este resultado elegí Not effective para todos de las preguntas: salida

 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 } });
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Mirando su salida, creo que está buscando replaceAll en lugar de replace .

Puede echarle un vistazo aquí https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll

about 4 years ago · Juan Pablo Isaza Relatório

0

.replace() devuelve una nueva cadena (no modifica la cadena existente)

Por lo tanto, deberá reemplazar y asignar nuevamente a la matriz de opciones. por ej. :

 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");

Demostración de trabajo:

 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 : en JavaScript, las cadenas son inmutables: una cadena existente nunca se modifica.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda