arrays සම්බන්ද, මේ JavaScript ගැටලුව විසද ගන්න උදව් ඕන.
-
මම API එකකින් response එක ගන්නවා. එකේන්, මේ පහල විධියට arrays 2ක් එක්ක response එක එනව.
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",…}
මේකේ, routesOutput කියන array එක ඇතුලේ weekly කියල තව array එකක් තියෙනව. ඒක ඇතුලේ flight_ids කියල එකක් තියෙනව. මේ පහල විධියට.
- routesOutput[0].schedules.weekly[0].flights.flight_ids
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
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":
මම for loops පාවිච්චි කරල , ඒවා loop කරල, arrays 2ම එක සමාන flight_ids තියේන ඒවා අරගෙන, ඒවා එක array එකකට push කරා. නමුත්, routesOutput[0].schedules.weekly[0].flights.flight_ids[0] හා [1] එකම element එකකට push කරන්න තමයි මට ඕන.
මෙහම තමයි මට ලැබුනේ.
[ { "ori_sub_origin": "CMB", "ori_sub_desti": "MLE" }, { "desti_sub_origin": "MLE", "desti_sub_desti": "IST" }, ]
මේ විධියටයි මට ඕන වෙන්නේ.
[ { "ori_sub_origin": "CMB", "ori_sub_desti": "MLE", "desti_sub_origin": "MLE", "desti_sub_desti": "IST" } ]
මෙන්න මගේ කෝඩ් එක.
for(i = 0; i < routes_with_weekly.length; i++) { let test_one = routes_with_weekly[i].flights.flight_ids[0]; let test_one_desti = routes_with_weekly[i].flights.flight_ids[1]; for(j = 0; j < schedulesOutput_let.length; j++) { if(schedulesOutput_let[j].flights[j] != undefined) { let test_two = schedulesOutput_let[j].flights; for(v = 0; v < test_two.length; v++) { let test_three = test_two[v].flight_ids; if(test_one == test_three) { let ori_all_data = { "ori_sub_origin" : schedulesOutput_let[j].departure_iata, "ori_sub_desti" : schedulesOutput_let[j].arrival_iata }; all_data_arr.push(ori_all_data); // break; } } } } } //desti for(r = 0; r < routes_with_weekly.length; r++) { let test_one = routes_with_weekly[r].flights.flight_ids[1]; for(t = 0; t < schedulesOutput_let.length; t++) { if(schedulesOutput_let[t].flights[t] != undefined) { let test_two = schedulesOutput_let[t].flights; for(b = 0; b < test_two.length; b++) { let test_three = test_two[b].flight_ids; if(test_one == test_three) { let desti_all_data = { "desti_sub_origin" : schedulesOutput_let[t].departure_iata, "desti_sub_desti" : schedulesOutput_let[t].arrival_iata }; all_data_arr.push(desti_all_data); // break; } } } } } console.log(JSON.stringify(all_data_arr , null, 2))
-
Object Assign method eka use karanna
karanna -
meken idea ekak ganna puluwan wei https://stackoverflow.com/questions/62949257/how-to-access-an-array-in-an-array-in-a-collection