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

183
Views
Object destructuring API data so it's easier to grab?
let pokeApi = ()=>{
  let randomize = Math.floor(Math.random() * 898);
  let url = `https://pokeapi.co/api/v2/pokemon/${randomize}`
  fetch(url)
    .then((res) => res.json())
    .then((pokeData) => {
      console.log(pokeData)
    })
}

I had this written before with myself manually entering pokeData to find the information. Example would be..

  pokeHeight.textContent = `Height: ${data.height} ft `;
  pokeWeight.textContent = `Weight: ${data.weight} KG `;
  1;
  pokeTemperment.textContent = `Type: ${data.types[0].type["name"]} `;
  spriteImage.src = data.sprites["front_shiny"];
  pokeName.textContent = data.name.toUpperCase();
  hp.textContent = `HP: ${data.stats[0]["base_stat"]}`;

But my programmer friend told me to try to object destructure it instead, as it's usually how he does it. I get what object destructuring technically is but I am not sure how I can set it up so it applies to the data properties.

const {height, weight, hp} = x

but how does the height in the const above = the fetch data?

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

0

You should apply destructing to the result of the fetch. You can do it like this:

let pokeApi = ()=>{
  let randomize = Math.floor(Math.random() * 898);
  let url = `https://pokeapi.co/api/v2/pokemon/${randomize}`
  fetch(url)
    .then((res) => res.json())
    .then(({height, weight}) => {
      console.log('Height: ', height);
      console.log('Weight: ', weight)
    })
}
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!