3

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.)?

4
  • 1
    Order cannot be guaranteed. You could use 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. Commented Dec 8, 2017 at 19:23
  • 2
    Javascript objects are maps, which by definition, are not indexed. Commented Dec 8, 2017 at 19:24
  • what about an array for the keys? Commented Dec 8, 2017 at 19:28
  • @NinaScholz That's what I'm working on using at the moment. I wasn't sure if there was an elegant way to do it as I described, and am hoping there still is - but for now, I'll be manipulating it into an array. Commented Dec 8, 2017 at 19:39

2 Answers 2

1

To iterate over the properties of an object in a consistent order, you have no choice but to use an array of the property names, for example:

var daysList = [ 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday' ];

And then you can iterate over the array of names:

for (let i = 0; i < daysList.length; i++) {
    let day = daysList[i];
    // do what you need with days[day]
}
Sign up to request clarification or add additional context in comments.

2 Comments

It does indeed seem that there is no way to do what I wanted in the manner I desired.
not quite as elegant as this i guess...but another way is Map
1

You can use Map - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

const days = { sunday: "N", monday: "Y", tuesday: "N", wednesday: "Y", 
thursday: "N", friday: "Y", saturday: "N" }

const map = new Map(Object.entries(days));
var mapIter = map.entries();

x = 0;
while(x < Object.keys(days).length) {
  let next = mapIter.next().value
  if(next[1] == 'Y') {
    console.log(next[0] + " is Y");
  }
  else if(next[1] == 'N') {
    console.log(next[0] + " is N");
  }
  x++;
}

3 Comments

Object.entries(days) does NOT guarantee the order: "The Object.entries() method returns an array of a given object's own enumerable property [key, value] pairs, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well)."
seems like it maintains order to me...look at the code snippet
One code snippet does not prove it, sorry. The order of for...in is NOT determined in JavaScript, even though many browsers preserve the order: "A for...in loop iterates over the properties of an object in an arbitrary order".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.