Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda