0

I'm trying to iterate over a nested object in Javascript and have the iterator return the sub-objects.

Here's the test data:

const testData = {
  item1: {
    name: "test",
    time: "now",
  },
  item2: {
    name: "other",
    time: "then",
  },
};

I'd like to do something like the following pseudocode with it:

for (var item in testData) {
  console.log(item.name);
  console.log(item.time);
};

which I want to have output:

"test"
"now"
"other"
"then"

Unfortunately, as simple as this feels like it ought to be, it seems to run afoul of type-related things going on in Javascript, and I'm quite at a loss as to how to handle it. How do I translate the above pseudocode for loop into functional Javascript?

Sjiveru
  • 107
  • 6
  • 4
    `testData[item].name` and `testData[item].time`. – Ivar Jul 19 '22 at 16:30
  • 1
    Does this answer your question? [Iterate through object properties](https://stackoverflow.com/questions/8312459/iterate-through-object-properties) – Ivar Jul 19 '22 at 16:31
  • @Ivar `for (item in testData) { console.log(testData[item].name) }` does it. Put that in an answer and I'll accept it! – Sjiveru Jul 19 '22 at 16:38

0 Answers0