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

170
Visualizações
Object destructuring won't take numbers?

    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((data) => {
          console.log(data)
          let {height, weight, stats:{0: {base_stat}} } = data;
        })
    }

   pokeApi();

stats is defined as an array with 6 objects. I'm trying to call line 0, and inside of 0 the name, but whenever I type a number it gives me an error I can't fix unless I delete the number.

How can I destructure so it equals this... data.stats[0]["base_stat"]

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To get the first element of array with the help of destructure, you can do like,

stats:[ {base_stat: firstBaseStat} ]

So the final destructuring will be like,

let {height, weight, stats:[ {base_stat: firstBaseStat} ] } = data;

And you can make use of firstBaseStat to get the first value.

Working Example:

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((data) => {
      let {height, weight, stats:[ {base_stat: firstBaseStat} ] } = data;
      console.log('height ', height, 'weight ', weight, 'first stats ', firstBaseStat)
    })
}

pokeApi();

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