Transfer a row of data from one object to another
My goal is to transfer a row of data from one object to another, where selected_city_codes
would state which items to remove from airport_data_2
to airport_data_1
by using city_id
Not sure how to approach appending the data from airport_data_2
to airport_data_1
but I illustrated my result to removing the row from airport_data_2
.
Is there a more efficient way to transfer data between two object?
airport_data_1 = [{"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"}];
airport_data_2 = [{"departure_time" :"03:00","arrival_time":"05:00","city_id":"DEN"},
{"departure_time" :"04:00","arrival_time":"06:00","city_id":"SJC"},
{"departure_time" :"04:00","arrival_time":"06:00","city_id":"JFK"},
{"departure_time" :"06:00","arrival_time":"09:00","city_id":"SJC"}];
selected_city_codes = ['SJC','JFK']; //items to remove from `airport_data_2` to `airport_data_1`
//my attempt to remove the listed values from selected_city_codes
for(i in selected_city_codes) {
airport_data_2 = airport_data_2.filter(item => item.city_id != selected_city_codes[i]);
}
Result Before
airport_data_1 = [{"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"}];
airport_data_2 = [{"departure_time" :"03:00","arrival_time":"05:00","city_id":"DEN"},
{"departure_time" :"04:00","arrival_time":"06:00","city_id":"SJC"},
{"departure_time" :"04:00","arrival_time":"06:00","city_id":"JFK"},
{"departure_time" :"06:00","arrival_time":"09:00","city_id":"SJC"}];
Result After -- our desired result
airport_data_1 = [{"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"},
{"departure_time" :"04:00","arrival_time":"06:00","city_id":"SJC"},
{"departure_time" :"04:00","arrival_time":"06:00","city_id":"JFK"},
{"departure_time" :"06:00","arrival_time":"09:00","city_id":"SJC"}];
airport_data_2 = [{"departure_time" :"03:00","arrival_time":"05:00","city_id":"DEN"}];
Did a slight change to your implementation and added the ones that are equal.
airport_data_1 = [{"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"}];
airport_data_2 = [{"departure_time" :"03:00","arrival_time":"05:00","city_id":"DEN"},
{"departure_time" :"04:00","arrival_time":"06:00","city_id":"SJC"},
{"departure_time" :"04:00","arrival_time":"06:00","city_id":"JFK"},
{"departure_time" :"06:00","arrival_time":"09:00","city_id":"SJC"}];
selected_city_codes = ['SJC','JFK'];
for(i in selected_city_codes) {
airport_data_2 = airport_data_2.filter((item) =>{
if (item.city_id === selected_city_codes[i]){
airport_data_1.push(item);
}
return item.city_id !== selected_city_codes[i]
});
}
console.log(airport_data_1)
console.log(airport_data_2)
.as-console-wrapper { max-height: 100% !important; top: 0; }