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

121
Visualizações
Using data with fetch - Vanilla JS

I can't transfer data I get from Fetch to a variable outside of fetch. First variable (currentExam) is an inside object and the second one (currentExam2) is an outside JSON. I want to do it with vanilla JS (no jQuery)

exams/example.json

[
  {
    "question": "What time is it",
    "options": ["23:04 UTC", "03:44 UTC", "23:04 UTC", "23:04 UTC"],
    "answer": 2,
    "explain": "",
    "image": ""
  },
  {
    "question": "What time is the Sunset?",
    "options": ["22:44 UTC", "22:45 UTC", "22:46 UTC ", "06:04 UTC"],
    "answer": 4,
    "explain": "",
    "image": "example.png"
  },
  {
    "question": "What time is it afterwards",
    "options": ["23:04 UTC", "03:44 UTC", "23:04 UTC", "23:04 UTC"],
    "answer": 2,
    "explain": "",
    "image": ""
  }
]

json-parsing.js

"use strict";

let currentExam = [
  {
    question: "What time is it",
    options: ["23:04 UTC", "03:44 UTC", "23:04 UTC", "23:04 UTC"],
    answer: 2,
    explain: "",
    image: "",
  },
  {
    question: "What time is the Sunset?",
    options: ["22:44 UTC", "22:45 UTC", "22:46 UTC ", "06:04 UTC"],
    answer: 4,
    explain: "",
    image: "example.png",
  },
];

let currentExam2 = [];
const json_url = "exams/example.json";

fetch(json_url)
  .then((response) => response.json())
  .then((data) => {
    const innerArr = Object.values(data);
    currentExam2.push(...innerArr);
  });

console.log(currentExam[0]);
console.log(currentExam2[0]);

As I run my code - I get this

{question: 'What time is it', options: Array(4), answer: 2, explain: '', image: ''}

undefined
about 4 years ago · Juan Pablo Isaza
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