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

171
Views
Cuestionario de matriz de clasificación de Javascript
const questions = [ { questionText: "What is the capital of France?", answerOptions: [ { answerText: "New York", isCorrect: false }, { answerText: "London", isCorrect: false }, { answerText: "Paris", isCorrect: true }, { answerText: "Dublin", isCorrect: false } ].sort(() => Math.random() - 0.5), //it sorts but when clicked it generate new random values(position change which is not good) in answerOptions1 answerOptions1: [ { answerText: "New York1", isCorrect: false }, { answerText: "London1", isCorrect: false }, { answerText: "Paris1", isCorrect: true }, { answerText: "Dublin1", isCorrect: false } ].sort(() => Math.random() - 0.5) //it sorts but when clicked it generate new random values in answerOptions }, {}, {} ] .sort(() => Math.random() - 0.5) // this sorting is not working ;

Enlace de Codesanbox: https://codesandbox.io/s/lucid-zeh-5ulr5z?file=/src/App.js

Necesito ordenar las preguntas, las opciones de respuesta y la matriz de opciones de respuesta1 en orden aleatorio. El .sort(() => Math.random() - 0.5) no funciona para esta lógica. Cuando se hace clic en answerOptions, también genera una nueva posición aleatoria en answeOptions1. answerOptions y answerOptions1 no deben cambiar su posición y datos/valor (cuando se obtienen de una lista grande).

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Su matriz generada aleatoriamente cambiará en cada renderizado. use el gancho React useMemo para generar una matriz aleatoria una vez.

 const questions = useMemo(()=>[ { questionText: "What is the capital of France?", answerOptions: useMemo(()=>[ { answerText: "New York", isCorrect: false }, { answerText: "London", isCorrect: false }, { answerText: "Paris", isCorrect: true }, { answerText: "Dublin", isCorrect: false } ].sort(() => Math.random() - 0.5), []), //it sorts but when clicked it generate new random values(position change which is not good) in answerOptions1 answerOptions1: useMemo(()=>[ { answerText: "New York1", isCorrect: false }, { answerText: "London1", isCorrect: false }, { answerText: "Paris1", isCorrect: true }, { answerText: "Dublin1", isCorrect: false } ].sort(() => Math.random() - 0.5), []) //it sorts but when clicked it generate new random values in answerOptions }, {}, {} ].sort(() => Math.random() - 0.5), [])
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!