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



Solution 1:[1]

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

Solution 2:[2]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ron Hillel
Solution 2 Wimanicesir