Trying to parse out airport data into two by using the array selected_city_codes
. So airport data would be into two parts, one with only the selected city codes and the other without the city codes.
I tried to attempt this using modify_airport_data
but for some reason airport_data_results
is not how I wanted it to be.
What would be the best approach to this problem?
airport_data = [{"departure_time":"12:00","arrival_time":"03:00","city_id":"BOS"},
{"departure_time" :"12:00","arrival_time":"03:00","city_id":"BOS"},
{"departure_time" :"01:00","arrival_time":"04:00","city_id":"SFO"},
{"departure_time" :"03:00","arrival_time":"05:00","city_id":"DEN"},
{"departure_time" :"03:00","arrival_time":"05:00","city_id":"BOS"},
{"departure_time" :"03:00","arrival_time":"05:00","city_id":"SFO"}];
selected_city_data = [];
selected_city_codes=['BOS','SFO'];
function modify_airport_data(airport_data, selected_city_data,selected_city_codes) {
parsed_airport_data = [];
var counter = 0; //for tracking purposes
for (j=0; j < airport_data.length; j++) {
for (i=0; i < selected_city_codes.length; i++) {
if (airport_data[j].city_id == selected_city_codes[i]) {
selected_city_data.push(airport_data[j]);
counter = 0; //we reset counter during match
}
else{
counter++; //and increment it if there is no match
}
if(counter == selected_city_codes.length) {
parsed_airport_data.push(airport_data[j]);
}
}
}
airport_data = parsed_airport_data;
return airport_data;
}
airport_data_results = modify_airport_data(airport_data, selected_city_data,selected_city_codes);
Actual Results
airport_data_results = [{departure_time: '01:00',arrival_time: '04:00',city_id: 'SFO' },
{departure_time : '03:00',arrival_time: '05:00',city_id: 'DEN' },
{departure_time : '03:00',arrival_time: '05:00',city_id: 'SFO' } ]
selected_city_data = [{departure_time: '12:00',arrival_time: '03:00',city_id: 'BOS' },
{departure_time : '12:00',arrival_time: '03:00',city_id: 'BOS' },
{departure_time : '01:00',arrival_time: '04:00',city_id: 'SFO' },
{departure_time : '03:00',arrival_time: '05:00',city_id: 'BOS' },
{departure_time : '03:00',arrival_time: '05:00',city_id: 'SFO' } ]
Desired Results
airport_data_results = [{departure_time : '03:00',arrival_time: '05:00',city_id: 'DEN' }]
selected_city_data = [{"departure_time":"12:00","arrival_time":"03:00","city_id":"BOS"},
{"departure_time" :"12:00","arrival_time":"03:00","city_id":"BOS"},
{"departure_time" :"01:00","arrival_time":"04:00","city_id":"SFO"},
{"departure_time" :"03:00","arrival_time":"05:00","city_id":"BOS"},
{"departure_time" :"03:00","arrival_time":"05:00","city_id":"SFO"}];