Skip to content
Advertisement

Accessing properties from object in `for`–`in` loop results in `undefined`

I have these two classes:

JavaScript

I’m simply trying to call printGraph to print all the nodeIds this way:

JavaScript

But it’s printing undefined. I can’t seem to figure out why it isn’t simply printing the nodeId.

Advertisement

Answer

you are using the wrong for loop. Try changing it to:

JavaScript

A for..of loop should loop over the node the way you want.
Result:

JavaScript
Advertisement