3

(I realize that iterating through an array with numeric indexes with for...in is bad practice (explained here) but I do not see how this is relevant here. I would like to understand why the problem described below happens. I lack the experience and skill to read through the CodeMirror source code to find out what went wrong unfortunately.)

Example app JSFiddle

There is a "2D array" generated here (array[]), 3 rows, each row consist of 4 elements (3 numbers and one \n character). My goal was to iterate through the whole "2D array" and put each character in the CodeMirror editor.

  • If you click on the "for...in" button, the code will do the first two rows of the array, then drops the following error:

Uncaught TypeError: Cannot read property 'chunkSize' of undefined

  • If you click on the "for...in that works" button, you will get the right result with no errors. The only difference between this and the previous function is that the "line" argument of the replaceRange method has a +1. This way it works perfectly for some reason
  • if you click on the "forEach" button, everything will work fine

I found about the fact that the "row" variable is the problem by trial and error. So my questions are:

  1. Why do I get the error above with the "for...in" button?
  2. Why don't I get the error with the "for...in that works" button?
digitaltos
  • 85
  • 7

0 Answers0