I have been jogging for about a year now, but as soon as I see multilevel cycles my head immediately smokes. How is it easier to understand them without straining the brain by 120%?)))

Closed due to the fact that it is necessary to reformulate the question so that the participants can give an objectively correct answer Vladimir Martyanov , user194374, Vartlok , PashaPash ♦ 5 Apr '16 at 8:48

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Very good reasons are usually needed to use more than 1 nested loop - iksuy
  • @iksuy, three-dimensional arrays generated?) - Sdafs Fasafs
  • On two nested for three-dimensional arrays, yes, almost an isolated case. Three nested for certain scientific calculations may apply. - iksuy pm

2 answers 2

Well, look: Let's say you have an object with friends. And each friend has several more friends. The task is that you need to display the user and his friends:

var user_friends = { Bob: ['July', 'John', 'Alex'], Alex: ['Bob', 'John'], July: ['Alex', 'Bob', 'John'], John: ['Bob', 'Alex', 'John'] }; 

Then you loop through the object in a loop:

 for(var user in user_friends) { var friends = user_friends[user]; //ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ frieds Π½Π° ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΡ‚Π΅Ρ€Π°Ρ†ΠΈΠΈ Π±ΡƒΠ΄Π΅Ρ‚ ΡΠΎΠ΄Π΅Ρ€ΠΆΠ°Ρ‚ΡŒ массив с Π΄Ρ€ΡƒΠ·ΡŒΡΠΌΠΈ } 

Then you add another loop to the loop, which will iterate over the array with friends:

 for(var user in user_friends) { //Π’Π΅Π»ΠΎ ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ Ρ†ΠΈΠΊΠ»Π° var friends = user_friends[user]; var friends_string = ''; for(var i=0; i<friends.length; i++) { //Π’Π΅Π»ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠ³ΠΎ Ρ†ΠΈΠΊΠ»Π° //Π—Π΄Π΅ΡΡŒ Π½Π° ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΡ‚Π΅Ρ€Π°Ρ†ΠΈΠΈ ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ Ρ†ΠΈΠΊΠ»Π° //Π‘ΡƒΠ΄Π΅ΠΌ ΠΏΠ΅Ρ€Π΅Π±ΠΈΡ€Π°Ρ‚ΡŒ массив fiends //И ΠΊΠΎΠ½ΠΊΠ°Ρ‚ΠΈΠ½ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ Π΅Π³ΠΎ Π² ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΡƒΡŽ: friends_string+=friends[i] + ' ,'; //Π—Π΄Π΅ΡΡŒ ΠΆΠ΅ Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Π² консоль: } console.log('Friends', 'of', user, 'is', friends_string); } 

In principle, it all depends on the structure, the data you want to iterate in cycles. But you need to avoid multi-level cycles, it is quite resource-intensive.

  • some not very example, the inner loop can be replaced by join(', ') - Grundy
  • @Grundy but from the point of view of implementation will not be the same - Risto
  • @Risto, most likely not. native array operations can be optimized. Well, in the code from the answer an extra comma is added at the end - Grundy
  • for (var i in array) should be used with should be used with the condition if (array.hasOwnProperty(i)){ ... } - Vasily Barbashev
  • @ Vasily Barbashev, in this case it is not necessary, it is just an extra operation, and it for..in not recommended to use with arrays in general for..in - Grundy

If you really have such complex cycles that your head is cracking and cannot be avoided, do this:

  • We take the inner loop, look at what data it needs for execution, put it into a function, with this data, as arguments.
  • We replace the inner loop with the resulting function in the program body.
  • Repeat the same operation for the parent loop, and so on until we get to the first level loop.

The advantages of this code:

  • Beauty and clarity: you do not need to search for the same cycle, or find out in which cycle you have one or another line of code, to suffer with indents to the floor line - all cycles are in separate function blocks.
  • Lack of messing with the counters: The counters in all cycles are local variables, you do not need to call them i , j , k , call i and do not suffer. If the inner loop needs to know the value of an external counter, pass it as an argument to the function, calling it at your discretion.