将某些行从一个对象移到另一个对象的最佳方法
Best way to move certain rows out of one object to another
只是想知道使用数组将数据从一个对象过滤到另一个对象的最佳方法是什么
我想将 selected_city_codes
中的整行从 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"},
{"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 = [];
selected_city_codes = ['SJC','JFK']; //items to remove from `airport_data_1` to `airport_data_2`
//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" :"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"}];
你必须过滤第一个数组并使用类似 includes
的东西来检查 city_id
是否在 selected_city_codes
:
const 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' },
];
const selected_city_codes = ['SJC', 'JFK'];
const airport_data_2 = airport_data_1.filter((el) =>
selected_city_codes.includes(el.city_id)
);
console.log(airport_data_2);
您可以利用 array.reduce() 将原始数组分成两部分。
const 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"}];
const selected_city_codes = ['SJC','JFK']; //items to remove from `airport_data_1` to `airport_data_2`
const data = airport_data_1.reduce((s, x) => {
s[ selected_city_codes.indexOf(x.city_id) < 0 ].push(x) ; return s;} , {true: [], false:[]} )
const data_1 = data.true;
const airport_data_2 = data.false;
console.log('airport_data_1 ', data_1);
console.log('airport_data_2 ', airport_data_2);
.as-console-wrapper { max-height: 100% !important; top: 0; }
只是想知道使用数组将数据从一个对象过滤到另一个对象的最佳方法是什么
我想将 selected_city_codes
中的整行从 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"},
{"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 = [];
selected_city_codes = ['SJC','JFK']; //items to remove from `airport_data_1` to `airport_data_2`
//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" :"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"}];
你必须过滤第一个数组并使用类似 includes
的东西来检查 city_id
是否在 selected_city_codes
:
const 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' },
];
const selected_city_codes = ['SJC', 'JFK'];
const airport_data_2 = airport_data_1.filter((el) =>
selected_city_codes.includes(el.city_id)
);
console.log(airport_data_2);
您可以利用 array.reduce() 将原始数组分成两部分。
const 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"}];
const selected_city_codes = ['SJC','JFK']; //items to remove from `airport_data_1` to `airport_data_2`
const data = airport_data_1.reduce((s, x) => {
s[ selected_city_codes.indexOf(x.city_id) < 0 ].push(x) ; return s;} , {true: [], false:[]} )
const data_1 = data.true;
const airport_data_2 = data.false;
console.log('airport_data_1 ', data_1);
console.log('airport_data_2 ', airport_data_2);
.as-console-wrapper { max-height: 100% !important; top: 0; }