gpt4 book ai didi

javascript - 将一行数据从一个对象传输到另一个对象

转载 作者:行者123 更新时间:2023-12-05 05:48:37 26 4
gpt4 key购买 nike

我的目标是将一行数据从一个对象传输到另一个对象,其中 selected_city_codes将说明要从 airport_data_2 中删除哪些项目至 airport_data_1通过使用 city_id

不确定如何附加来自 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"}];

Result After -- 我们想要的结果

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_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; }

关于javascript - 将一行数据从一个对象传输到另一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70781368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com