Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda