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
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 conditionif (array.hasOwnProperty(i)){ ... }- Vasily Barbashev- @ Vasily Barbashev, in this case it is not necessary, it is just an extra operation, and it
for..innot recommended to use with arrays in generalfor..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, calliand 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.