In my code I have 6 lists of objects of different sizes.
I need to go through them all in a specific order, from the smallest list to the largest.
JavaScript
x
15
15
1
var list_1 = [ ] // length 24
2
var list_2 = [ ] // length 4
3
var list_3 = [ ] // length 3
4
var list_4 = [ ] // length 4
5
var list_5 = [ ] // length 11
6
var list_6 = [ ] // length 2
7
8
// Need code here for loop each list in order asc
9
list_6.forEach( ) // length 2
10
list_3.forEach( ) // length 3
11
list_2.forEach( ) // length 4
12
list_4.forEach( ) // length 4
13
list_5.forEach( ) // length 11
14
list_1.forEach( ) // length 24
15
Does anyone have a simple solution ? Thanks
Advertisement
Answer
You could add the lists in an array, sort it and perform the loop
JavaScript
1
4
1
[list, list2, ]
2
.sort((a, b) => a.length - b.length)
3
.forEach(array => array.forEach( ))
4