Goal is to filter out data using the array selected_bank_ids
which will contain the bank id
and/or the last 3 digits for 401
accounts.
One issue I have is removing an entire row of bank_data
that contains the digit string "401" within selected_bank_ids
What would be the best way to approach this problem?
bank_data = [{"arrival_time":"12:00","amount":"900","id":"BS"},
{"arrival_time" :"12:00", "amount":"300", "id":"BD" },
{"arrival_time" :"01:00", "amount":"400", "id":"SW" },
{"arrival_time" :"02:00", "amount":"800", "id":"OS-401" },
{"arrival_time" :"03:00", "amount":"500", "id":"SF" },
{"arrival_time" :"04:00", "amount":"600", "id":"SJ" },
{"arrival_time" :"04:30", "amount":"700", "id":"SJ" },
{"arrival_time" :undefined,"amount":undefined,"id":undefined },
{"arrival_time" :"04:00", "amount":"600", "id":"FS-401" },
{"arrival_time" :"05:00", "amount":"600", "id":"FS" },
{"arrival_time" :"06:00", "amount":"900", "id":"JC" }];
upcoming_bank_data = [];
//Goal is to grab any item within bank_id list or ids that end in 401
selected_bank_ids = ['BS','BD','SJ', 'FS', '401'];
for(i in selected_bank_ids) {
is_401 = false;
bank_data = bank_data.filter((item) =>{
if(item.id != undefined) {
parser = selected_bank_ids[i].substring(selected_bank_ids[i].length - 3);
//would make sure that bank_id would always include 401 towards the end
is_401 = item.id.includes(parser);
}
if (item.id === selected_bank_ids[i] || is_401) {
upcoming_bank_data.push(item);
is_401 = false;
}
return item.id !== selected_bank_ids[i];
});
}
/*
console.log('upcoming_bank_data');
console.log(upcoming_bank_data);
console.log('-----------------------');
console.log('bank_data');
console.log(bank_data);
*/
Current result
bank_data
should not contain a row that has the string digit 401 within the id
upcoming_bank_data
[ { arrival_time: '12:00', amount: '900', id: 'BS' },
{ arrival_time: '12:00', amount: '300', id: 'BD' },
{ arrival_time: '04:00', amount: '600', id: 'SJ' },
{ arrival_time: '04:30', amount: '700', id: 'SJ' },
{ arrival_time: '04:00', amount: '600', id: 'FS-401' },
{ arrival_time: '05:00', amount: '600', id: 'FS' },
{ arrival_time: '02:00', amount: '800', id: 'OS-401' },
{ arrival_time: '04:00', amount: '600', id: 'FS-401' } ]
-----------------------
bank_data
[ { arrival_time: '01:00', amount: '400', id: 'SW' },
{ arrival_time: '02:00', amount: '800', id: 'OS-401' },
{ arrival_time: '03:00', amount: '500', id: 'SF' },
{ arrival_time: undefined, amount: undefined, id: undefined },
{ arrival_time: '04:00', amount: '600', id: 'FS-401' },
{ arrival_time: '06:00', amount: '900', id: 'JC' } ]
Desired result
upcoming_bank_data
[ { arrival_time: '12:00', amount: '900', id: 'BS' },
{ arrival_time: '12:00', amount: '300', id: 'BD' },
{ arrival_time: '02:00', amount: '800', id: 'OS-401' },
{ arrival_time: '04:00', amount: '600', id: 'SJ' },
{ arrival_time: '04:30', amount: '700', id: 'SJ' },
{ arrival_time: '04:00', amount: '600', id: 'FS-401' },
{ arrival_time: '05:00', amount: '600', id: 'FS' }]
-----------------------
bank_data
[ { arrival_time: '01:00', amount: '400', id: 'SW' },
{ arrival_time: '03:00', amount: '500', id: 'SF' },
{ arrival_time: undefined, amount: undefined, id: undefined },
{ arrival_time: '06:00', amount: '900', id: 'JC' } ]
CodePudding user response:
You could reduce the array and take a partition for a
and b
.
const
bank_data = [{ arrival_time: "12:00", amount: "900", id: "BS" }, { arrival_time: "12:00", amount: "300", id: "BD" }, { arrival_time: "01:00", amount: "400", id: "SW" }, { arrival_time: "02:00", amount: "800", id: "OS-401" }, { arrival_time: "03:00", amount: "500", id: "SF" }, { arrival_time: "04:00", amount: "600", id: "SJ" }, { arrival_time: "04:30", amount: "700", id: "SJ" }, { arrival_time: undefined, amount: undefined, id: undefined }, { arrival_time: "04:00", amount: "600", id: "FS-401" }, { arrival_time: "05:00", amount: "600", id: "FS" }, { arrival_time: "06:00", amount: "900", id: "JC" }],
selected_bank_ids = ['BS','BD','SJ', 'FS', '401'],
[a, b] = bank_data.reduce((r, o) => {
const check = selected_bank_ids.includes(o.id) || (o.id || '').slice(-3) === '401';
r[ check].push(o);
return r;
}, [[], []]);
console.log(b);
console.log(a);
.as-console-wrapper { max-height: 100% !important; top: 0; }
CodePudding user response:
I think what you are looking for might be just one line of code
bank_data = [{ arrival_time: '01:00', amount: '400', id: 'SW' },
{ arrival_time: '02:00', amount: '800', id: 'OS-401' },
{ arrival_time: '03:00', amount: '500', id: 'SF' },
{ arrival_time: undefined, amount: undefined, id: undefined },
{ arrival_time: '04:00', amount: '600', id: 'FS-401' },
{ arrival_time: '06:00', amount: '900', id: 'JC' }];
const filtered_bank_data = bank_data.filter(e => !e.id || !e.id.endsWith('401'))
console.log(filtered_bank_data);
So, you are filtering all elements in the bank_data array which doesn't have id (it's undefined) or which id doesn't end with 401
CodePudding user response:
Another way to filter the array excluding ids with 401 at the end:
const upcomingBankData = [{ arrival_time: '01:00', amount: '400', id: 'SW' }, { arrival_time: '02:00', amount: '800', id: 'OS-401' }, { arrival_time: '03:00', amount: '500', id: 'SF' }, { arrival_time: undefined, amount: undefined, id: undefined }, { arrival_time: '04:00', amount: '600', id: 'FS-401' }, { arrival_time: '06:00', amount: '900', id: 'JC' }]
const bankData = upcomingBankData.filter(a => !/401$/.test(a.id))
console.log(bankData)
.as-console-wrapper { max-height: 100% !important; top: 0; }
CodePudding user response:
You can achieve this with a simple way by iterating the input array and via use of String.includes()
& Array.includes()
.
Working Demo :
// Input Data
const bank_data = [{"arrival_time":"12:00","amount":"900","id":"BS"},
{"arrival_time" :"12:00", "amount":"300", "id":"BD" },
{"arrival_time" :"01:00", "amount":"400", "id":"SW" },
{"arrival_time" :"02:00", "amount":"800", "id":"OS-401" },
{"arrival_time" :"03:00", "amount":"500", "id":"SF" },
{"arrival_time" :"04:00", "amount":"600", "id":"SJ" },
{"arrival_time" :"04:30", "amount":"700", "id":"SJ" },
{"arrival_time" :undefined,"amount":undefined,"id":undefined },
{"arrival_time" :"04:00", "amount":"600", "id":"FS-401" },
{"arrival_time" :"05:00", "amount":"600", "id":"FS" },
{"arrival_time" :"06:00", "amount":"900", "id":"JC" }];
const selected_bank_ids = ['BS','BD','SJ', 'FS', '401'];
// Result : Filtered data
const res = bank_data.filter((obj) => {
return (obj.id?.includes('-')) ?
selected_bank_ids.includes(obj.id.split('-')[1]) :
selected_bank_ids.includes(obj.id)
});
console.log(res);