gen.dev
gen.dev

Reputation: 25

Javascript Filter trying to avoid multiple if statements

Hi I have to filter an array based on some conditions. I want to avoid having to write out a bunch if statements. My code below shows pretty much what I want to accomplish. The * you see is just me trying to say filter to any (like a wild card). Do you have any idea how I can best accomplish my goal? Thank you

let x =eq.filter(eq => eq.empid == selectedTech === 'ALL Techs'? '*' : selectedTech 
    &&  eq.code == selectedStatus === 'ALL Statuses'? '*' : selectedStatus 
    &&  eq.cellnum == phn === '' ? '*': phn
    &&  eq.jobid == jobid === '' ? '*': jobid
    );

Example of the object:

var eq = [{
  empid: 1,
  jobid: 27,
  code: "Not Started",
  cellnum: "3058888888",
  brand: "GK",
  model: "X500"
}, {
  empid: 1,
  jobid: 33,
  code: "Not Started",
  cellnum: "3058888899",
  brand: "Mackie",
  model: "X500"
}, {
  empid: 2,
  jobid: 35,
  code: "In Progress",
  cellnum: "3058888877",
  brand: "Samson",
  model: "X522"
}, {
  empid: 1,
  jobid: 36,
  code: "Not Started",
  cellnum: "3058888866",
  brand: "Mackie",
  model: "X467"
}, {
  empid: 1,
  jobid: 37,
  code: "In Progress",
  cellnum: "3058888800",
  brand: "Fender",
  model: "X500"
}]
}

Upvotes: 0

Views: 118

Answers (1)

Barmar
Barmar

Reputation: 780843

You don't need '*', use eq.whatever there since it will be guaranteed to compare equal to itself.

And use parentheses to ensure that the precedence is what you want. I'm not sure offhand how the == precedence compares to the tertiary.

let x = eq.filter(eq => eq.empid == (selectedTech === 'ALL Techs' ? eq.empid : selectedTech) &&
  eq.code == (selectedStatus === 'ALL Statuses' ? eq.code : selectedStatus) &&
  eq.cellnum == (phn === '' ? eq.cellnum : phn) &&
  eq.jobid == (jobid === '' ? eq.jobid : jobid)
);

Maybe a clearer way to do it is either compare the filter variable to its wildcard value or the object property.

var eq = [{
  empid: 1,
  jobid: 27,
  code: "Not Started",
  cellnum: "3058888888",
  brand: "GK",
  model: "X500"
}, {
  empid: 1,
  jobid: 33,
  code: "Not Started",
  cellnum: "3058888899",
  brand: "Mackie",
  model: "X500"
}, {
  empid: 2,
  jobid: 35,
  code: "In Progress",
  cellnum: "3058888877",
  brand: "Samson",
  model: "X522"
}, {
  empid: 1,
  jobid: 36,
  code: "Not Started",
  cellnum: "3058888866",
  brand: "Mackie",
  model: "X467"
}, {
  empid: 1,
  jobid: 37,
  code: "In Progress",
  cellnum: "3058888800",
  brand: "Fender",
  model: "X500"
}];

let selectedTech = 'ALL Techs',
  selectedStatus = 'In Progress',
  phn = '3058888800',
  jobid = 37;

let x = eq.filter(eq =>
  (selectedTech === 'ALL Techs' || eq.empid == selectedTech) &&
  (selectedStatus === 'ALL Statuses' || eq.code == selectedStatus) &&
  (phn === '' || eq.cellnum == phn) &&
  (jobid === '' || eq.jobid == jobid)
);

console.log(x);

phn = "123456789";

x = eq.filter(eq =>
  (selectedTech === 'ALL Techs' || eq.empid == selectedTech) &&
  (selectedStatus === 'ALL Statuses' || eq.code == selectedStatus) &&
  (phn === '' || eq.cellnum == phn) &&
  (jobid === '' || eq.jobid == jobid)
);

console.log(x);

Upvotes: 1

Related Questions