Skip to content
Advertisement

Loop multiple lists based on their length [closed]

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.

var list_1 = [...]    // length 24
var list_2 = [...]    // length 4
var list_3 = [...]    // length 3
var list_4 = [...]    // length 4
var list_5 = [...]    // length 11
var list_6 = [...]    // length 2

// Need code here for loop each list in order asc
list_6.forEach(...)   // length 2
list_3.forEach(...)   // length 3
list_2.forEach(...)   // length 4
list_4.forEach(...)   // length 4
list_5.forEach(...)   // length 11
list_1.forEach(...)   // length 24

Does anyone have a simple solution ? Thanks

Advertisement

Answer

You could add the lists in an array, sort it and perform the loop

[list, list2, ...]
    .sort((a, b) => a.length - b.length)
    .forEach(array => array.forEach(...))
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement