I want to loop through an array of projects, filter out elements with multiple conditions, and push them to the destinated array. The result should have 4 arrays containing all the projects that met each specific array's condition. Something like this
// 4 cases of the parent array
const arrayOverDeadline_overBudget = []
const arrayOverDeadline_withinBudget = []
const arrayOneMonthDeadline_OverBudget = []
const arrayOneMonthDeadline_withinBudget = []
export const filterArray = (array) => {
const today = moment();
const oneMonthBefore = moment().add(1, "m");
array.forEach((item) => {
switch (item.deadline && item.budget) {
// over deadline && over budget
// (deadline <= today) && (budget > 100%)
case params.deadline <= today && params.budget >= 100:
break;
// over deadline && within budget
// (deadline <= today) && (80% < budget < 100%)
case params.deadline < today &&
params.budget < 100 &&
params.budget > 80:
break;
// within deadline && within budget
// (today < deadline < oneMonthBefore) && (80% <= budget < 100%)
case params.deadline > today &&
params.deadline < oneMonthBefore &&
params.budget < 100 &&
params.budget >= 80:
break;
// within deadline && over budget
// (today < deadline < oneMonthBefore) && (budget >= 100%)
case params.deadline > today &&
params.deadline < oneMonthBefore &&
params.budget > 100:
break;
default:
break;
}
});
};
I looked for some answers and found Flags and Bitmasks but wasn't sure how to implement it in my cases. I also worry about the time complexity of my method. Any tips or ideas would be very welcome and appreciated.
Update:
if..elseif...else approach but I just wanted to know if there are "cleaner" ways, and it doesn't have to use switch statement.You can simply break this down to 2 boolean variables:
bool1 = is project over the deadlinebool2 = is project over the budgetCreate an int from these variables (bool1 right shifted once + bool2) to get a value between [0,3], and then you can map this value to the target array.
Example:
bool1 = 0, bool2 = 0. Target value = 00 (binary) = 0. Target value 0 maps to under deadline, under budget.bool1 = 0, bool2 = 1. Target value = 01 (binary) = 1. Target value 1 maps to under deadline, over budget.bool1 = 1, bool2 = 0. Target value = 10 (binary) = 2. Target value 2 maps to over deadline, under budget.bool1 = 1, bool2 = 1. Target value = 11 (binary) = 3. Target value 3 maps to over deadline, over budget.You can generate this target value using just one line of code, and then using these cases to decide the destination array should remove the need for writing complicated if-else blocks.
Adding a sample implementation:
const budget = 100,
deadline = 100
const item_budget = 120,
item_deadline = 80
const b1 = Number(item_deadline > deadline)
const b2 = Number(item_budget > budget)
const target = (b1 << 1) + b2
switch (target) {
case 0:
console.log("under deadline, under budget")
break;
case 1:
console.log("under deadline, over budget")
break;
case 2:
console.log("over deadline, under budget")
break;
default:
console.log("over deadline, over budget")
}
// prints "under deadline, over budget"