parsing out data from an object into two other objects by using an array
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"}];
You could simply reduce the array and move either the object to the one who match the wanted city or not.
const
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_codes = ['BOS', 'SFO'],
[airport_data_results, selected_city_data] = airport_data.reduce((r, o) => {
r[+selected_city_codes.includes(o.city_id)].push(o);
return r;
}, [[], []]);
console.log(airport_data_results);
console.log(selected_city_data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You could use the array find method as a function for filtering the array:
const 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"}];
const selected_city_codes = ['BOS','SFO'];
const finder = (city_id) => selected_city_codes.find( (city) => city_id === city );
const airport_data_results = airport_data.filter( (flight) => !finder(flight.city_id) )
const selected_city_data = airport_data.filter( (flight) => finder(flight.city_id) )
console.log(airport_data_results, selected_city_data)
UPDATE:
or shorter:
const 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"}];
const fnd = id => ['BOS','SFO'].find( cid => id === cid );
const airport_data_results = airport_data.filter( row => !fnd(row.city_id) )
const selected_city_data = airport_data.filter( row => fnd(row.city_id) )
console.log(airport_data_results, selected_city_data)