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

232
Visualizações
How can I find the value from another value in the same line with JavaScript (JSON)?

I am trying to get the "correctAnswers" from a number.

Here a is sample of the JSON:

{
  "questions": [
    {
      "number": 3,
      "question": "☀️ ➕ 🌼 = ?",
      "answers": [
        "🌻sunflower",
        "BIRTHDAY",
        "iPhone",
        "MOON CAKE"
      ],
      "correctAnswers": [
        "🌻sunflower"
      ],
      "random": true,
      "timeLimit": "24"
    },
    {
      "number": 1,
      "question": "⭐️ ➕ 🐟 =❓ ",
      "answers": [
        "STAR FISH",
        "iPhone",
        "MOON CAKE",
        "HOT DOG"
      ],
      "correctAnswers": [
        "STAR FISH"
      ],
      "random": true,
      "timeLimit": "20"
    },
    {
      "number": 7,
      "question": "👁 ➕ ☎ = ",
      "answers": [
        "SUNGLASSES",
        "SUNFLOWER",
        "HOUSEPAINT",
        "IPHONE"
      ],
      "correctAnswers": [
        "IPHONE"
      ],
      "random": true,
      "timeLimit": 15
    },
  ]
}

I am trying to find "number":1, OR a random number inside of the "questions" and then find the "correctAnswers" from that number.

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

0

const array = {
  "questions": [
    {
      "number": 3,
      "question": "☀️ ➕ 🌼 = ?",
      "answers": [
        "🌻sunflower",
        "BIRTHDAY",
        "iPhone",
        "MOON CAKE"
      ],
      "correctAnswers": [
        "🌻sunflower"
      ],
      "random": true,
      "timeLimit": "24"
    },
    {
      "number": 1,
      "question": "⭐️ ➕ 🐟 =❓ ",
      "answers": [
        "STAR FISH",
        "iPhone",
        "MOON CAKE",
        "HOT DOG"
      ],
      "correctAnswers": [
        "STAR FISH"
      ],
      "random": true,
      "timeLimit": "20"
    },
    {
      "number": 7,
      "question": "👁 ➕ ☎ = ",
      "answers": [
        "SUNGLASSES",
        "SUNFLOWER",
        "HOUSEPAINT",
        "IPHONE"
      ],
      "correctAnswers": [
        "IPHONE"
      ],
      "random": true,
      "timeLimit": 15
    },
  ]
};

const { questions } = array;

// Find by number
const correctAnswersByNumber = qNumber => {
  const q = questions.find(({ number }) => number === qNumber);
  if (!q) {
    return false;
  }

  return q.correctAnswers;
};
console.log(correctAnswersByNumber(3)); // Question by number === 3

// Random question
const randomCorrectAnswers = questions[Math.floor(Math.random() * questions.length)].correctAnswers;
console.log(randomCorrectAnswers);

about 4 years ago · Juan Pablo Isaza Relatório

0

Choose a random question using random. Then show the correct properties.

var data = {
  "questions": [
    {
      "number": 3,
      "question": "☀️ ➕ 🌼 = ?",
      "answers": [
        "🌻sunflower",
        "BIRTHDAY",
        "iPhone",
        "MOON CAKE"
      ],
      "correctAnswers": [
        "🌻sunflower"
      ],
      "random": true,
      "timeLimit": "24"
    },
    {
      "number": 1,
      "question": "⭐️ ➕ 🐟 =❓ ",
      "answers": [
        "STAR FISH",
        "iPhone",
        "MOON CAKE",
        "HOT DOG"
      ],
      "correctAnswers": [
        "STAR FISH"
      ],
      "random": true,
      "timeLimit": "20"
    },
    {
      "number": 7,
      "question": "👁 ➕ ☎ = ",
      "answers": [
        "SUNGLASSES",
        "SUNFLOWER",
        "HOUSEPAINT",
        "IPHONE"
      ],
      "correctAnswers": [
        "IPHONE"
      ],
      "random": true,
      "timeLimit": 15
    },
  ]
};


// Let's choose a random question
let question = data['questions'][Math.floor(Math.random() * data['questions'].length)];


console.log(question.question, question.correctAnswers)

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