0

无需过多介绍细节,我的问题是,您将如何减少 NodeJS 中的重复?我是一个初学者,所以请怜悯。

我得到一个包含信息的 api,并输出我自己的 api,即根据 req.query 参数(从、到、日期)过滤和排序的信息。

这是有效的代码,但其中有太多重复:

app.get('/search', async (req, res, next) => {
try {
    const apiRes = await axios.get('https://thistheapi.net/api/TravelPrices');
    result = apiRes.data;
    searchFrom = req.query.from;
    searchTo = req.query.to;
    searchDate = req.query.date;
    const routesArray = [];
    for (let route of result) { routesArray.push(route) };

    if (searchFrom.toLowerCase() == "mercury" && searchTo.toLowerCase() == "earth") {

        finalResult = [];

        // Finding and filtering the first flight

        const fromFilterF1 = "Mercury";

        // Create an array, which has the results of routes that match the req.query from name/ aka starting point

        firstArrayF1 = routesArray.filter(obj => obj.routeInfo.from.name == fromFilterF1);
        const toFilterF1 = "Venus";

        // Filter the resulting array with the next 'to' location

        secondArrayF1 = firstArrayF1.filter(obj => obj.routeInfo.to.name == toFilterF1);

        // Create an array that has all the providers with their data for this specific route / flight
        const providerArrayF1 = secondArrayF1.map(x => x.providers)
        const trialArrayF1 = [];
        
        for (let x of providerArrayF1) { for (let y of x) { trialArrayF1.push(y) } }

        // Use the req.query selected date to filter all flights that match the date

        dateFilterF1 = { flightStart: searchDate };

        // options for the date variable, since in the api data it has specific time of day also added

        const options = { year: 'numeric', month: 'numeric', day: 'numeric' };
        thirdArrayF1 = trialArrayF1.filter(obj => new Date(obj.flightStart).toLocaleDateString('en-CA', options) == dateFilterF1.flightStart);

        // Sort the resulting array of matching from-location, to-location, and date - starting from the earliest flights to the latest one

        thirdArrayF1.sort((a, b) => { return new Date(a.flightStart) - new Date(b.flightStart) });

        finalResult.push(thirdArrayF1[0]);

        // ALL OF THIS REPEATS FOR THE SECOND & THIRD FLIGHT, except the flight start date/time has to be later than the flight end time of the previous flight

        // Finding and filtering the second flight
        if (thirdArrayF1.length == 0) { finalResult.push(null) } else {
            const fromFilterF2 = "Venus";
            firstArrayF2 = routesArray.filter(obj => obj.routeInfo.from.name == fromFilterF2);
            const toFilterF2 = "Earth";
            secondArrayF2 = firstArrayF2.filter(obj => obj.routeInfo.to.name == toFilterF2);
            const providerArrayF2 = secondArrayF2.map(x => x.providers)
            const trialArrayF2 = [];
            for (let x of providerArrayF2) { for (let y of x) { trialArrayf2.push(y) } }
            dateFilterF2 = { flightStart: thirdArrayF1[0].flightEnd };
            thirdArrayF2 = trialArrayF2.filter(obj => new Date(obj.flightStart).toLocaleDateString('en-CA', options) >= dateFilterF2.flightStart);
            thirdArrayF2.sort((a, b) => { return new Date(a.flightStart) - new Date(b.flightStart) });
            finalResult.push(thirdArrayF2[0])
        };
            // Finding and filtering the third flight
            if (thirdArrayF2.length == 0) { finalResult.push(null) } else {
                const fromFilterF3 = "Earth";
                firstArrayF3 = routesArray.filter(obj => obj.routeInfo.from.name == fromFilterF3);
                const toFilterF3 = "Jupiter";
                secondArrayF3 = firstArrayF3.filter(obj => obj.routeInfo.to.name == toFilterF3);
                const providerArrayF3 = secondArrayF3.map(x => x.providers)
                const trialArrayF3 = [];
                for (let x of providerArrayF3) { for (let y of x) { trialArrayF3.push(y) } }
                dateFilterF3 = { flightStart: thirdArrayF2[0].flightEnd };
                thirdArrayF3 = trialArrayF3.filter(obj => new Date(obj.flightStart).toLocaleDateString('en-CA', options) >= dateFilterF3.flightStart);
                thirdArrayF3.sort((a, b) => { return new Date(a.flightStart) - new Date(b.flightStart) });
                finalResult.push(thirdArrayF3[0])
            };
        res.json(finalResult);

        } else if (searchFrom.toLowerCase() == "mercury" && searchTo.toLowerCase() == "jupiter"){ etc...

如您所见,有很多类似的代码,但我不知道如何在不破坏代码并停止工作的情况下使其更紧凑、更少重复。

我感谢所有的帮助和建议!

此外,这是我使用的 api 片段:

"legs":[{"id":"a0ee2c2b-667c-46d7-87c0-2ca32da88a46","routeInfo":{"id":"44edd88d-8904-4266-9df5-f37701741123","from":{"id":"0ee3379b-98fb-4b46-9aef-0a3a81a46ad4","name":"Earth"},"to":{"id":"a504bf72-2be2-4f2b-bab1-61d818757e3a","name":"Jupiter"},"distance":628730000},"providers":[{"id":"0257eab0-7c5c-4a4c-af79-cdf6f3ab9349","company":{"id":"27b1ce2f-c88a-45f4-96e1-dd9fcbb2db73","name":"Spacegenix"},"price":570774.60,"flightStart":"2022-02-04T07:17:16.4529653Z","flightEnd":"2022-02-08T13:57:16.4529653Z"},{"id":"e6ed4071-e29c-46a1-a38f-a082eff0e4de","company":{"id":"eb12838f-afb4-4447-9781-2d87b0641337","name":"Galaxy Express"},"price":180679.62,"flightStart":"2022-02-13T00:30:16.4529883Z","flightEnd":"2022-02-17T14:29:16.4529883Z"} et cetera.

基本上我正在不同地点之间进行自定义连接航班。我确信有一种方法可以减少重复性,但我无法弄清楚。

4

0 回答 0