for x in y loop behaviour


    Jul 08 2013 | 4:09 pm
    Hi all,
    Why, unlike Python for example, the following post(item) code outputs the index of each item in the list, not their value.
    function test() { var list = ['a','b','c','d']; for (item in list) { post(item); } }

    • Jul 08 2013 | 8:22 pm
      In javascript, you'd want:
      function test() { var list = ['f','b','c','d']; for (item in list) { post(list[item]); } }
    • Jul 08 2013 | 8:50 pm
      Hey mike,
      Thanks for the link! I know how to get the right output, I'm just curious why the implementation is different from python.
      ps. looking forward to ICMC ;)
    • Jul 16 2013 | 9:16 am
      for (var item in list) would be preferable also...
    • Jul 17 2013 | 8:57 am
      good point, lee, for (var item in list) to make sure that the variable item is not shared with other javascripts