Good day. Gentlemen, I'm desperate. There is:

var subparts=[ [0,'Раздел_1',10,0], [1,'Раздел_2',10,1], [2,'Раздел_3',10,2], ... ]; var items=[ [0,'Пункт_1','параметр_1','параметр_2',10,0], [1,'Пункт_2','параметр_1','параметр_2',10,1], [2,'Пункт_3','параметр_1','параметр_2',10,2], ... ]; for (var k =0, subpart_id, i=0; k < subparts.length; k++) { console.log(subparts[k][1]); for (; k == items[i][5]; i++) { console.log(' '+items[i][1]); } } 

As I want - a walk through the array, if the subparts contains some items from items, they are listed below. What turns out - this is how it turns out, but for some reason the console gives out “Uncaught TypeError: Cannot read property '5' of undefined” (and this: fire in jquery-1.8.3.js: 974 , self.fireWith in jquery-1.8 .3.js: 1084 , jQuery.extend.ready in jquery-1.8.3.js: 406 , DOMContentLoaded in jquery-1.8.3.js: 83 ). What does this mean?

  • you would use named fields instead of arrays, it would be more readable. - VladD
  • Yes, but somehow I have already begun, now I will not begin to redo it. Maybe in the new version. And the same can not affect the appearance of an error? - Realetive__

2 answers 2

I did not understand the condition of the second cycle at all. Here is a normal comparison (just substitute the indices of the array elements being compared are correct):

 var subparts=[ [0,'Раздел_1',10,0], [1,'Раздел_2',10,1], [2,'Раздел_3',10,2] ]; var items=[ [0,'Пункт_1','параметр_1','параметр_2',10,0], [1,'Пункт_2','параметр_1','параметр_2',10,1], [2,'Пункт_3','параметр_1','параметр_2',10,2] ]; for (var k =0, subpart_id; k < subparts.length; k++) { console.log(subparts[k][1]); for (var i=0; i < items.length; i++) { if (items[i][5]==subparts[k][0]) console.log(' '+items[i][1]); } } 

    In your code, i only increases at each iteration of k , and is never reset. No wonder that it eventually becomes more than the number of items in items , after which items[i] is undefined, and items[i][5] throw an exception.

    Do not despair, just reset i at the beginning of each iteration of k .

    • And how, sorry, her (i) "reset"? More precisely where? - Realetive__