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

88
Views
tab of object undefined in Js

I'm currently facing a problem :

After fetching an API response request I wanted to store those informations in tab.

I decided to create an object to get an object tab and after do whatever I want with.

The problem is that when I display the tab I can see what is inside, but when i want to get tab[0] for example, it displays me 'undefined'. I'm quite blocked at this point since i don't really know why my tab[O] is undefined...

Here's the code i use :

let tab = [];

fetch("http://127.0.0.1:8000/api/assets").then(response => {
    response.json().then(data => {
        for(let i = 0 ; i < data.length ; i++){
            let obj = {
                logo: data[i]["symbol"],
                name: data[i]["name"],
                price: data[i]["price"]
            }
            tab.push(obj);
        }
    });
}).catch(
    err => {
        console.log("err: " + err)
    })

console.log(tab)
console.log(tab.length)
console.log(tab[0])

Console display

general.js:218 = console.log(tab)

general.js:219 = console.log(tab.length)

general.js:220 = console.log(tab[0])

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You are printing your tab object to the console before the request has had time to finish. Try moving the console.log() statements into your request script after it has received the data:

let tab = [];

fetch("http://127.0.0.1:8000/api/assets").then(response => {
    response.json().then(data => {
        for(let i = 0 ; i < data.length ; i++){
            let obj = {
                logo: data[i]["symbol"],
                name: data[i]["name"],
                price: data[i]["price"]
            }
            tab.push(obj);
        }
        console.log(tab)
        console.log(tab.length)
        console.log(tab[0])
    });
}).catch(
    err => {
        console.log("err: " + err)
    })
about 4 years ago · Juan Pablo Isaza Report

0

You are checking it before the fetch returns, you need to do it after like such:

let tab = [];

fetch("http://127.0.0.1:8000/api/assets")
  .then(response => {
    response.json().then(data => {
      for (let i = 0; i < data.length; i++) {
        let obj = {
          logo: data[i]["symbol"],
          name: data[i]["name"],
          price: data[i]["price"]
        };
        tab.push(obj);
      }
    })
  })
  .then(function(response) {
    console.log(tab);
    console.log(tab.length);
    console.log(tab[0]);
  })
  .catch(
    err => {
      console.log("err: " + err);
    });

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