While looping through an object is simple using for(key in object), I'd like to access an object via an index (like an array) rather than its value.
I have a "days of the week" object composed of:
days: { sunday: "N", monday: "Y", tuesday: "N", wednesday: "Y",
thursday: "N", friday: "Y", saturday: "N" }
I want to use a for-loop which cycles through the seven days of the week (0-6) and checks whether the object is "Y" or "N" for that day (checking the "days" object for the value at key 0 (sunday), 1 (monday), 2, 3, etc.)
for (var i = 0; i < 7; i++) { }
My problem could be solved with a bunch of if statements if i == 0 { //check sunday } else if i == 1 { //check monday } or manipulating my object into multiple arrays and going from there, however neither of these are very elegant. Is there a way to loop through the days object and access each value by a key (0 for first, 1 for second, etc.)?
Object.keys(daysObject)for collecting all the day properties in an array, and after that you could iterate over it. Again, I am saying order cannot be guaranteed.