Questions Arrays දෙකක, එක සමාන elements හොයා ගන්නේ කොහමද ??

Seven Net

Active member
  • Jan 13, 2014
    290
    62
    28
    30
    Earth
    මම API එකකින් response එක ගන්නවා. එකේන්, මේ පහල විධියට arrays 2ක් එක්ක response එක එනව.

    JSON:
    routesOutput: [{origin_iata: "CMB", destination_iata: "IST",…}, {origin_iata: "CMB", destination_iata: "SAW",…}]
        0: {origin_iata: "CMB", destination_iata: "IST",…}
        1: {origin_iata: "CMB", destination_iata: "SAW",…}
    schedulesOutput: [{departure_iata: "CMB", arrival_iata: "MLE",…}, {departure_iata: "MLE", arrival_iata: "IST",…},…]
        0: {departure_iata: "CMB", arrival_iata: "MLE",…}
        1: {departure_iata: "MLE", arrival_iata: "IST",…}

    Node Arr 1.PNG

    මේකේ, routesOutput කියන array එක ඇතුලේ weekly කියල තව array එකක් තියෙනව. ඒක ඇතුලේ flight_ids කියල එකක් තියෙනව. මේ පහල විධියට.

    * routesOutput[0].schedules.weekly[0].flights.flight_ids

    JSON:
    routesOutput: [{origin_iata: "CMB", destination_iata: "IST",…}, {origin_iata: "CMB", destination_iata: "SAW",…}]
        schedules: {updated: {}, weekly: [{routing_id: "159058258847310869390999", frquency: [1, 1, 0, 1, 1, 0, 1],…},…]}
            weekly: [{routing_id: "159058258847310869390999", frquency: [1, 1, 0, 1, 1, 0, 1],…},…]
                0: {routing_id: "159058258847310869390999", frquency: [1, 1, 0, 1, 1, 0, 1],…}
                    technical_stops: "MLE"
                        flights: {flight_ids: ["159058258847310869501898", "159058258847310869627899"]}
                            flight_ids: ["159058258847310869501898", "159058258847310869627899"]
                                0: "159058258847310869501898"
                                1: "159058258847310869627899"

    ඒ වගේම, schedulesOutput කියන array එක ඇතුලේත් flight_ids කියල තියේනව. මේ පහල විධියටම.

    * schedulesOutput[0].flights.flight_ids

    JSON:
    schedulesOutput: [{departure_iata: "CMB", arrival_iata: "MLE",…}, {departure_iata: "MLE", arrival_iata: "IST",…},…]
        0: {departure_iata: "CMB", arrival_iata: "MLE",…}
            flights: [{flight_ids: "159058258847310869501898", operating_iata: "TK",…},…]
                0: {flight_ids "159058258847310869501898", operating_iata: "TK",…}
                    flight_ids: "159058258847310869501898":

    මට ඕන, මේ routesOutput හා schedulesOutput කියන arrays දෙකේ තියේන flight_ids සමාන වෙනවද කියල බලලා, ඒ flight id එකට විතරක් අදාල technical_stops එක console කර ගන්නයි.

    මම මේ, පහල විධියට කරත්, මට ඕන දේ වෙන්නේ නැ. මම කොහමද මේක හදා ගන්නේ ??

    JavaScript:
    let routesMap = await schedulesAdd.mapSchedules();
        let routesOutput_let = routesMap.routesOutput;
        let schedulesOutput_let = routesMap.schedulesOutput;
        let routes_with_weekly = routesOutput_let[0].schedules.weekly;
        let flights_from_schedule = [];
        let route_with_flight_ids = [];
        let schedule_with_flight_ids = [];
    
        routes_with_weekly.forEach(routes_with_weekly_element => {
            let test_one = routes_with_weekly_element.flights.flight_ids
            route_with_flight_ids.push(test_one);
        });
    
        schedulesOutput_let.forEach(schedulesOutput_let_element => {
            let test_two = schedulesOutput_let_element.flights;
            flights_from_schedule.push(test_two);
        });
    
        flights_from_schedule.forEach(flights_from_schedule_element => {
            let test_two_point_one = flights_from_schedule_element[0].flight_ids;
            schedule_with_flight_ids.push(test_two_point_one);
        });
    
        for(let i = 0; i < route_with_flight_ids.length; i++) {
            if(schedule_with_flight_ids != undefined) {
                if(route_with_flight_ids[i][0] == schedule_with_flight_ids[i]) {
                    console.log(route_with_flight_ids[i][0]);
                }
            }
        }
     

    theavidgamer

    Member
    Sep 25, 2017
    19
    15
    3
    JavaScript:
    let f = [];
    
    routesOutput.forEach(route=>{
        route.schedules.weekly.forEach(week=>{
            f.push({id: week.flights.flight_ids, stop:week.technical_stops});
        });
    });
    
    let s = [];
    schedulesOutput.forEach(schedule=>{
        schedule.flights.forEach(flight => {
            s.push(flight.flight_ids);
        });
    });
    
    
    let filtered = f.filter(x => {
        return s.includes(x.id);
    });


    Try this. I haven't tested though.