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

198
Views
¿Cómo contar todas las rutas posibles de un cuestionario?

Tengo un cuestionario en el que las preguntas posteriores dependen de la respuesta del usuario. Necesito contar la cantidad de rutas de cuestionario posibles, averiguar la cantidad de rutas y agregar todas las rutas de cuestionario a la matriz. ¿Cómo puedo hacer esto?

En mi código, cambio la estructura de las preguntas y agrego una matriz con respuestas y preguntas de seguimiento:

 const newQuestionObj = {}; const getAllPath = (arr) => { const treeStructure = arr.forEach((item) => { newQuestionObj[item.id] = { ...item, children: [ ...Object.keys(item) .filter((k) => k.includes("answer")) .map((k) => ({ ...item[k] })), ], }; }); console.log(newQuestionObj); }; getAllPath(questions);
 <script> const questions = [{ id: "1", question: "q1", answer_1: { text: "a1", next_question: "2", }, answer_2: { text: "a2", next_question: "3", }, }, { id: "2", question: "q2", answer_1: { text: "a1", next_question: "", }, answer_2: { text: "a2", next_question: "", }, }, { id: "3", question: "q3", answer_1: { text: "a1", next_question: "", }, answer_2: { text: "a2", next_question: "4", }, }, { id: "4", question: "q4", answer_1: { text: "a1", next_question: "", }, answer_2: { text: "a2", next_question: "", }, }, ]; </script>

Necesito obtener un objeto como este:

 { paths: { number: 3, list: [ [{ "q1": "a1" }, { "q2": "a1/a2" } ], [{ "q1": "a2" }, { "q3": "a1" } ], [{ "q1": "a2" }, { "q3": "a2" }, { "q4": "a1/a2" } ], ] } }
about 4 years ago · Juan Pablo Isaza
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!