Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
Using data with fetch - Vanilla JavaScript

I can't transfer data I get from Fetch to a variable outside of fetch. First variable (currentExam) is an inside object and the seconed 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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda