我的目标是将一行数据从一个对象传输到另一个对象,其中
selected_city_codes
将说明要使用city_id
从airport_data_2
中删除哪些项以将其添加到airport_data_1
中。不确定如何处理将数据从airport_data_2
追加到airport_data_1
的问题,但我演示了从airport_data_2
删除行的结果。是否有更有效的方法在两个对象之间传输数据?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]);
}
之前的结果
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"}];
在 -- 我们期望的结果之后的结果
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"}];
airport_data_2
,并使用if else
条件语句创建两个新数组。一个将成为新的airport_data_2
,第二个将与airport_data_2
进行concat()
操作。但我怀疑这对性能影响不大。 - Lluser