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

169
Vistas
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 Respuestas
Responde la pregunta

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