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

139
Vistas
rest api get an array of javascript objects

I am consuming this public api, the problem I have is that it is selecting an array of objects within the object field and I cannot access these values in the console, it shows me this (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]

const url = https://catfact.ninja/facts;

    const getFact = () => {
    return fetch('https://catfact.ninja/facts' ,
    {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json'
        }})
    .then(res => res.json())
    }
    const createFactDiv = (fact) => {
        const factContainer = document.createElement('div');
        const setup = document.createElement('p');
      
        setup.innerText = fact.data
        console.log(fact.data);
        factContainer.append(setup);
      
        return factContainer
      }
    const appendFact = (factDiv) => {
    const factContainer = document.getElementById('factContainer');
    factContainer.append(factDiv);
    }

    //This is unused
    /*
    document.addEventListener('DOMContentLoaded', () => {
    })
    */

    getFact().then ((fact) => {
    const factDiv = createFactDiv(fact);
    appendFact(factDiv);
    })

enter image description here

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can do a console.log(JSON.stringify(fact.data))

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think you're getting tripped up because getFact() is actually returning a result set that contains a number of (potentially useful) properties. It should probably be called getFacts(), because the .data property contains an array of multiple facts.

Perhaps try looping over the data array and setting setup.innerText to fact.fact rather than fact.data?

const getFacts = () => {
    return fetch('https://catfact.ninja/facts' ,
        {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            }}).then(res => res.json())
    }

const createFactDiv = (fact) => {
    const factContainer = document.createElement('div');
    const setup = document.createElement('p');
  
    setup.innerText = fact.fact;
    console.log(fact.fact);
    factContainer.append(setup);
  
    return factContainer
  }

const appendFact = (factDiv) => {
    const factContainer = document.getElementById('factContainer');
    factContainer.append(factDiv);
}

//This is unused
/*
    document.addEventListener('DOMContentLoaded', () => {
    })
*/

getFacts().then(facts => facts.data.forEach(fact => appendFact(createFactDiv(fact))))
about 4 years ago · Juan Pablo Isaza Denunciar

0

console.log(JSON.stringify(fact.data));
about 4 years ago · Juan Pablo Isaza Denunciar
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