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

226
Visualizações
how to access object data in node.js?

This is my object.

players:{
   "ssa": {
     "roomCode": "SJRJzaGA8",
     "imagesAlloted": [],
     "team": "",
     "sessionId": "0gSfuhvVF"
   },
   "ss": {
     "roomCode": "SJRJzaGA8",
     "imagesAlloted": [],
     "team": "",
     "sessionId": "8G7QtTEXV"
   }
}

I want to iterate whole object with keys and values

I am using

for(let [key, value] in players){
   console.log(key + "has" + JSON.stringify(value));
}

this is giving output similar to:

[0] "$" is "c"
[0] "$" is "i"
[0] "t" is "o"
[0] "c" is "l"
[0] "t" is "r"

I am on node version 16.13

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

0

I would make my for loop like this

for (let player in players) {
  console.log("Key: ", player, " has ", players[player]);
}

player is the key

players[player] will give you the value of that player.

Your way

for (const [key, value] of Object.entries(object1)) {
  console.log("Key: ", key, " has ", value);
}

should also give you the same result. Here's the MDN reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

about 4 years ago · Juan Pablo Isaza Relatório

0

The other solutions proposed in this thread work, but I feel OP is making a basic mistake with the syntax of his for/in loop, and it's important for him/her to understand why the initial code failed. It is because there should not be brackets around [key, value]. To do what OP wants with a for/in loop, the syntax is:

for(let p in players){
   console.log(p, players[p]);
}

Output:

ssa {roomCode: 'SJRJzaGA8', imagesAlloted: Array(0), team: '', sessionId: '0gSfuhvVF'}
ss {roomCode: 'SJRJzaGA8', imagesAlloted: Array(0), team: '', sessionId: '8G7QtTEXV'}

The reason you got single letters is that your incorrect [key, value] fragment is destructuring each key. When you attempt to destructure a string into an array, the string will break down into individual characters. So given the statement const [foo, bar, baz] = "ABC" will assign A to foo, B to bar, and so on. Your original code iterates over the object, plucks out each key as a string, assigns the first and second letters of that string to variables, and then logs the variables.

about 4 years ago · Juan Pablo Isaza Relatório

0

Use Object.entries() to get the keys and values.

Object.entries(players).forEach(([key, value]) => console.log(key + "has" + JSON.stringify(value)))
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