This is some code I wrote to evaluate a form with structured data in it on the client side, attempting to build the same object heirarchy Ruby on Rails would build once the form is submitted.
- at line 10 I was previously using for (var n in names), and this actually caused the code in line 21 to fail unexpectedly with the array lookup always returning null. The fact using the for…in construct on an array does not give you the value but does not give you an incrementable index either was something I had no reason to anticipate.
- A well known type checking error I had forgotten about since last time it bit me: typeof Array returns ‘object’ and not ‘array’. Unbelievably dumb. Hence my use of the helper function is_array in line 14.
That both these problems were occurring at the same time made it very very difficult for me to see why the code was failing, until I had added debug trace messages to practically every line to try to work out what was going wrong.
It doesn’t help that I am also quite sleepy and haven’t washed in a few days since my hot water system failed on the weekend– It’s now fixed and I am about to enjoy a good long shower.