Code review: Can this be simplified?

Still struggling with this massive amount of gtfs data that thwarted every attempt to process it automatically. But eventually seeing some light at the end of the tunnel. So I’m coming back to the issue of filtering the data.
I really like the idea of sorting the array by length as you did and refactored my code accordingly. But I did not understand how sorting the array ahead of time is useful in your example. You use a for loop, so you loop through each item anyway, what did I miss here?
I absolutely like the idea though and extended my second approach with this feature using ids rather than names:

trainLines.sort((a, b) => b.length - a.length);

const trainLineStopIdsArrCopy = => =>;

        const returnArrayOfTrainStopIds = arr => =>;

        const uniqueJourneys = trainLines.filter((trainLineArr, index) => 
            !(returnArrayOfTrainStopIds(trainLineArr).every(trainLineStop => 
                trainLineStopIdsArrCopy.slice(0, index).find(trainLineStopArrCopy => // slicing the array so I have to iterate only over the previous shorter arrays

It is sorted in-place first by length. Then 2 new arrays are built up by looping through the sorted array. Since the longest lines are first, the arrays being built up will contain the longest lines. Then as shorter lines are compared with the longer lines already stored, they will only be added if they have at least one different station. Probably not the most efficient way but you may only need to run this once in the program.

1 Like