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

178
Visualizações
How to get object property list data

I have a list of objects. Every object has a property which is a list of elements:

  { name       : 'Club 01'
  , id         : 1
  , form       : 45
  , points     : 0
  , tactics    : 'neutral'
  , played     : 0
  , gameset    : 0
  , playedWith : [ 8, 1, 2, 3 ] 
  } 

I want to go through the list and console log all existing elements:

for (let a = 0; a<clubs.width; a++) {
  for (let b = 0; b<clubs[a].playedWith.width; b++) {
    console.log(clubs[a].playedWith[b]);
  }
}

when i do it for one item, this works. however when i do it with a loop as aboce, this brings me to

undefined

Whats wrong with my code? How do i console log all items within playedWith property?

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

0

Elikill58 is right. Arrays have length property, not width property.

So your code would work well this way:

for (let a = 0; a < clubs.length; a++){
  for (let b = 0; b < clubs[a].playedWith.length; b++){
    console.log(clubs[a].playedWith[b]);
  }
}

Also, if you want to iterate through all items in the array, just for the sake of simplicity, you can write it like so:

for (const club of clubs) {
  for (const width of club.playedWith) {
    console.log(width);
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

let b = { name: 'Club 01', id: 1, form: 45, points: 0, tactics: 'neutral', played: 0, gameset: 0, playedWith: [8, 1, 2, 3],

move: function() {
    return `
    ${ this.name }and
    ${this.id }and
    ${ this.form }and
    ${ this.points }and
    ${ this.tactics }and
    ${ this.played }and
    ${ this.gameset }and
    ${ this.playedWith }`
}

};

console.log(b.move()) for (var w in b) { console.log(${w}:${b.move()}) }

about 4 years ago · Juan Pablo Isaza Relatório

0

You have to use length instead of width for both loop.

Here is an example :

 var clubs = [
 { name       : 'Club 01'
  , id         : 1
  , form       : 45
  , points     : 0
  , tactics    : 'neutral'
  , played     : 0
  , gameset    : 0
  , playedWith : [ 8, 1, 2, 3 ] 
  } 
];

for (let a = 0; a < clubs.length; a++) {
  for (let b = 0; b < clubs[a].playedWith.length; b++) {
    console.log(clubs[a].playedWith[b]);
  }
}

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