0

I am working with an api that gives data about transportation schedules, and the response is organized by dates with times nested inside the dates. A response looks something like the following:

object = {
  2016-07-27: {
    09:30:00: {
      // data here
    }
    13:00:00: {
      // data here
    }
  }
  2016-07-28: {
    09:30:00: {
      // data here
    }
    13:00:00: {
      // data here
    }
  }
}

I want to be able to get into the time objects to get the data I need.

Thank you!

wariofan1
  • 481
  • 2
  • 4
  • 17

2 Answers2

2
Object.keys(object).forEach(date => Object.keys(object[date]).forEach(time => {
    const value = object[date][time]; // values iteration
}))
Maxx
  • 1,740
  • 10
  • 17
1

This is a simple example of how you would iterate through an object's properties.

for (var property in object) {
    if (object.hasOwnProperty(property)) {
        // do stuff
    }
}

hasOwnProperty is necessary because an object's prototype contains additional properties for the object which are technically part of the object.

Source: Iterate through object properties

Community
  • 1
  • 1
Dennis
  • 3,962
  • 7
  • 26
  • 44