While troubleshooting a script I was writing for use with an Electron app (a user script in BitBurner), I discovered that an object I received from the app’s API exhibited surprising behavior. My initial code had this form:
for (const [name, value] in theObject) {
console.log(name + ": " + value);
}
, except that it was outputting via an app-specific API rather than via console.log()
.
I found that I was getting the expected number of properties, of the expected types, but neither the names nor the values themselves were what I expected.
However, when I switched to Object.entries()
:
for (const [name, value] of Object.entries(theObject)) {
console.log(name + ": " + value);
}
, I did get the results I expected. I could run those two variants one after the other on the same object, and they consistently produced different output from each other.
Was I wrong to be surprised by that difference?
How might it have arisen, and should I routinely be concerned about seeing such differences in other contexts?