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

199
Vistas
Trouble understanding how to implement .map()

Only a few months in to a dev program and I'm an idiot so bear with me.

I'm attempting to build a jeopardy app using http://jservice.io/. The function below first makes a get request to the URL, then uses _.sampleSize to get 6 random responses from the API, then iterates through NUM_CATEGORIES to get the ids (which are saved to the categoryID variable) before pushing those ids to the catId array. Also, please do correct me if my understanding of how that works is completely off.

let catId = [];

async function getCategoryIds() {
    const res = await axios.get(`https://jservice.io/api/categories/?count=50`);
    NUM_CATEGORIES = _.sampleSize(res.data, [n = 6]);

    for (let num of NUM_CATEGORIES) {
        let categoryID = num.id;
        catId.push(categoryID);
    }
    console.log(catId);
}
getCategoryIds();

The next part of the project is to return an object with data about a category like so:

Returns { title: "Math", clues: clue-array }

  • Where clue-array is:
  • [
  •  {question: "Hamlet Author", answer: "Shakespeare", showing: null},
    
  •  {question: "Bell Jar Author", answer: "Plath", showing: null},
    
  • ]

In the function below I've attempted to use .map() to iterate over the catId array and return the data I need but honestly, I'm just completely lost here (which is why I didn't bother moving on to the "clues" part). Can someone help explain to me how I can use map to make this work?

async function getCategory(catId) {
    const res = await axios.get(`http://jservice.io/api/clues?category=${catId}`);
    catId.map(result => {
        return {
            question: res.data.question,
            answer: res.data.answer,
            title: res.data.title,
        }
    });
    console.log(catId);
}

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

0

IIRC correctly Axios returns an object with a data property. So you need to use map to iterate over data, and return a new object with each element's values.

async function getCategory(catId) {

  const res = await axios.get(`http://jservice.io/api/clues?category=${catId}`);

  const out = res.data.map(el => {
    return {
      question: el.question,
      answer: el.answer,
      title: el.title,
    }
  });

  console.log(out);

}

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