S.Chandra Sekhar
S.Chandra Sekhar

Reputation: 503

Adding conditions to filter() dynamically in Javascript

I want to add conditions in JavaScript filter() method dynamically.

I have the code below:

let condition = '';
let a = ['empEmail', 'employeeAction', 'transactionNo', 'deviceListName', 'projectName'];

if (this.selectedEmployeeAlias != undefined) {
  condition += '&& a => a.empEmail === this.selectedEmployeeAlias';
}
if (this.employeeStatusList != undefined) {
  condition += '&& a.employeeAction === this.employeeStatusList'
}
if (this.selectedTransactionNo != undefined) {
  condition += '&& a.transactionNo === this.selectedTransactionNo';
}
if (this.selectedDeviceList != undefined) {
  condition += ' && a.deviceListName == this.selectedDeviceList';
}
if (this.selectedProjectName != undefined) {
  condition += '&& a.projectName == this.selectedProjectName';
}

var finalCondition = condition.substring(2, condition.length);
var fArray = arrayDetails.filter(finalCondition);

The code is returning an error as:

finalCondition is not a function.

Could you please let me know how can I add conditions to filter() dynamically.

Upvotes: 0

Views: 669

Answers (3)

Nina Scholz
Nina Scholz

Reputation: 386560

You could take an array of functions with conditions. Then iterate with every.

var conditions = [];

if (this.selectedEmployeeAlias !== undefined) {
    conditions.push(a => a.empEmail === this.selectedEmployeeAlias);
}
if (this.employeeStatusList !== undefined) {
    conditions.push(a => a.employeeAction === this.employeeStatusList);
}
if (this.selectedTransactionNo !== undefined) {
    conditions.push(a => a.transactionNo === this.selectedTransactionNo);
}
if (this.selectedDeviceList !== undefined) {
    conditions.push(a => a.deviceListName == this.selectedDeviceList);
}
if (this.selectedProjectName !== undefined) {
    conditions.push(a => a.projectName == this.selectedProjectName);
}

var fArray = arrayDetails.filter(o => conditions.every(c => c(o)));

Upvotes: 2

Jonas Wilms
Jonas Wilms

Reputation: 138257

As you got the nakes of the keys, just loop over them and check for undefineds:

const keys = ['empEmail', 'employeeAction', 'transactionNo', 'deviceListName', 'projectName'];

const result = arrayDetails.filter(el => {
   for(const key of keys) {
     if(this[key] === undefined) continue;
     if(this[key] !== el[key]) return false;
   }
   return true;
});

Upvotes: 0

Andria
Andria

Reputation: 5075

eval to the Rescue!

While it's generally advised against, eval does exactly what you want here.

Just pass your condition variable to eval inside the .filter method and voila!

let condition='';
let a = ['empEmail', 'employeeAction', 'transactionNo', 'deviceListName', 'projectName'];

if (this.selectedEmployeeAlias != undefined) {                    
  condition += '&& a => a.empEmail === this.selectedEmployeeAlias';
}
if (this.employeeStatusList != undefined) {
  condition += '&& a.employeeAction === this.employeeStatusList'
}
if (this.selectedTransactionNo != undefined) {
  condition += '&& a.transactionNo === this.selectedTransactionNo';
}
if (this.selectedDeviceList != undefined) {
  condition += ' && a.deviceListName == this.selectedDeviceList';
}
if (this.selectedProjectName != undefined) {
  condition += '&& a.projectName == this.selectedProjectName';
}

var finalCondition=condition.substring(2, condition.length);
var fArray=arrayDetails.filter(stuff => eval(finalCondition));

Upvotes: -1

Related Questions