1

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

Anand
  • 75
  • 1
  • 8
  • 1
    Checkout the MDN docs for `for ... in` loops, they start with an example of basically exactly this. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in – Cal Irvine Dec 16 '21 at 19:25
  • these won't work @CalIrvine – Anand Dec 16 '21 at 19:30

4 Answers4

0

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

Object.entries(players).forEach(([key, value]) => console.log(key + "has" + JSON.stringify(value)))
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You could use Object.entries() and flatMap()

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

const result = Object.values(players).flatMap((v) => {
    return Object.entries(v).flat();
});

console.log(result);
axtck
  • 3,707
  • 2
  • 10
  • 26
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

devSwap
  • 61
  • 5
  • these are workin in js console but not on node server side – Anand Dec 16 '21 at 19:57
  • maybe it's because it is a deep object, so on the console it probably is just displaying `[object object]` or something but it does exists (if that makes sense) – devSwap Dec 16 '21 at 20:00
  • i did Json.stringify to solve [object][object]. the object is having environmental keys too.I want the one is specified – Anand Dec 16 '21 at 20:21
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.

Matt Korostoff
  • 1,927
  • 2
  • 21
  • 26
  • these are workin in js console but not on node server side – Anand Dec 16 '21 at 19:57
  • I don't know what you mean by "not working" but here is a screenshot of this code pasted line for line into a nodejs (v16) server environment https://imgur.com/a/3Bfq4CB – Matt Korostoff Dec 16 '21 at 20:55