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

179
Visualizações
Javascript sorting array quiz
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
; 

codesanbox link: https://codesandbox.io/s/lucid-zeh-5ulr5z?file=/src/App.js

I need to sort questions, answerOptions and answerOptions1 array in random order. The .sort(() => Math.random() - 0.5) is not working for this logic. When answerOptions is clicked, it also generate new random position in answeOptions1. answerOptions and answerOptions1 should not change their position and data/value(when fetch from large list).

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your randomly generated array will change in each render. use React useMemo hook to generate random array once.

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 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