It can be beneficial to write additional code to gain a deeper understanding of the process.
An initial suggestion is to elaborate on the content within your if
statement like this:
Keep in mind that using a simple ||
operator instead of ternary is also effective.
PS: I have used poor variable naming without considering the purpose behind your actions. When reusing the following code, ensure to change them and provide comments explaining your intentions.
const isA = [
'ALL',
'',
].includes(this.accountnumber.value) || ele.accountnumber === this.accountnumber.value;
const isB = [
'ALL',
'',
].includes(this.description.value) || ele.description === this.description.value;
const isC = [
'ALL',
'',
].includes(this.country.value) || ele.country === this.country.value;
const isD = !this.entryDate || this.entryDate === dateEntry;
const isE = !this.editedDate || this.editedDate === dateEdited;
if (isA && isB && isC && isD && isE) {
return true;
}
Expanding upon this will highlight duplicated sections of code.
function checkCond(key, obj, arr = [
'ALL',
'',
]) {
return arr.includes(this[key].value) || obj[key] === this[key].value;
}
const isA = checkCond('accountnumber', ele);
const isB = checkCond('description', ele);
const isB = checkCond('country', ele);
const isD = !this.entryDate || this.entryDate === dateEntry;
const isE = !this.editedDate || this.editedDate === dateEdited;
if (isA && isB && isC && isD && isE) {
return true;
}
Taking it one step further:
function checkCond(key, obj, arr = [
'ALL',
'',
]) {
return arr.includes(this[key].value) || obj[key] === this[key].value;
}
const conditions = [
checkCond('accountnumber', ele),
checkCond('description', ele),
checkCond('country', ele),
!this.entryDate || this.entryDate === dateEntry,
!this.editedDate || this.editedDate === dateEdited,
];
if (conditions.every(x => x)) {
return true;
}