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

202
Views
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 answers
Answer question

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 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!