Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!