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

346
Vistas
How do I target a specific key in an array of objects for a conditional inside a for loop?

New to JS! Trying to specify a statement for one of my keys inside my object, which again is nested in an array. Next to Slowbro - the message: "Wow this is a big Pokemon" should be displayed but this is not working. I am trying to write a general conditional within my for loop for all Pokemons but also specify that the "Wow this is a big Pokemon" message should only appear for Slowbro and not just any who meet the height>1.5 criteria. What did I miss?

//Array contains Pokemon data to display in application
//Each object represents one Pokemon with respective properties
let pokemonList = [
        {name:'Pikachu', height: 0.4, type:['electric','ground','steel','flying']},
        {name:'Charmeleon', height: 1.1, type:['ground','rock', 'water']},
        {name: 'Slowbro' ,height: 1.6, type:['electric', 'grass','dragon','ghost','bug']}
      ];

//Loop lists each Pokemon in array by assigning name and height keys 
for (let i=0; i < pokemonList.length; i++) 
  { document.write(pokemonList[i].name+ ' (height: ' + pokemonList[i].height + ') ' + '<br>')
  }



for (let i=0; i < pokemonList[i].length; i++) {
  if (pokemonList[i].height > 1.5 && pokemonList[i].name === 'Slowbro') {
    console.log('- Wow, this is a big Pokemon!');
    } else if (pokemonList[i].height > 0.5 && pokemonList[i].height < 1.5) {
    console.log('- This is an average size Pokemon.');
    } else {
    console.log('- This is a small Pokemon.')
    }
}

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Try this

//Array contains Pokemon data to display in application
//Each object represents one Pokemon with respective properties
let pokemonList = [
        {name:'Pikachu', height: 0.4, type:['electric','ground','steel','flying']},
        {name:'Charmeleon', height: 1.1, type:['ground','rock', 'water']},
        {name: 'Slowbro' ,height: 1.6, type:['electric', 'grass','dragon','ghost','bug']}
      ];

const html = pokemonList.map(({name, height}) => {
   return `${name} (height: ${height})`
}).join('<br>')

document.write(html)

pokemonList.forEach(({name, height}) => {
  if( height > 1.5){
    console.log('- Wow, this is a big Pokemon!')
  }else if(height > 0.5){
    console.log('- This is an average size Pokemon.');
  } else {
    console.log('- This is a small Pokemon.')
  }
})

about 4 years ago · Juan Pablo Isaza Denunciar

0

In the last for loop in your code snippet, instead of the length of the array, you are using the length of its first element:

for (let i=0; i < pokemonList[a].length; i++)

Where you probably want the length of the array itself:

for (let i=0; i < pokemonList.length; i++)

instead.

//Array contains Pokemon data to display in application
//Each object represents one Pokemon with respective properties
let pokemonList = [
        {name:'Pikachu', height: 0.4, type:['electric','ground','steel','flying']},
        {name:'Charmeleon', height: 1.1, type:['ground','rock', 'water']},
        {name: 'Slowbro' ,height: 1.6, type:['electric', 'grass','dragon','ghost','bug']}
      ];

//Loop lists each Pokemon in array by assigning name and height keys 
for (let i=0; i < pokemonList.length; i++) 
  { document.write(pokemonList[i].name+ ' (height: ' + pokemonList[i].height + ') ' + '<br>')
  }



for (let i=0; i < pokemonList.length; i++) {
  if (pokemonList[i].height > 1.5 && pokemonList[i].name === 'Slowbro') {
    console.log('- Wow, this is a big Pokemon!');
    } else if (pokemonList[i].height > 0.5 && pokemonList[i].height < 1.5) {
    console.log('- This is an average size Pokemon.');
    } else {
    console.log('- This is a small Pokemon.')
    }
}

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