0

If I do something like this, and then enumerate inputMenu with the loop, is it guaranteed that my for loop will return them in the order I added them? Or is this not guaranteed? Thank you.

inputMenu = {
            IDOD: { text: "Diam (1=ID, 0=OD)", value: null },
            DIA: { text: "Diameter", value: null },
            THK: { text: "Segment Thickness", value: null },
            PLATEWIDTH: { text: "Plate Width", value: null },
            SEG_NEUT_AXIS: { text: "MID-ARC Length", value: null } };

for (var key in inputMenu) {    
   // Will I get IDOD -> DIA -> THK -> PLATEWIDTH -> SEG_NEUT_AXIS every time?
};

2 Answers2

0

If you want guaranteed order, you should use an array. The order in which properties appear in a for in loop is not guaranteed.

Bjorn
  • 69,215
  • 39
  • 136
  • 164
0

No : the order is not specified and is implementation dependent.

From the ECMAScript specification :

The mechanics and order of enumerating the properties (step 6.a in the first algorithm, step 7.a in the second) is not specified.

Use an array if the order is important for you.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758