Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!