0

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"
}]
}
2
  • Whitespace is your friend, if nothing else. Commented Mar 2, 2021 at 22:12
  • 1
    What are you trying to do with eq.jobid == jobid === '' ? '*': jobid? Commented Mar 2, 2021 at 22:19

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

11 Comments

@jarmod No, == is correct. We're comparing a property in eq to either a filter variable or itself.
@jarmod Take a look at the update, it may make it clearer.
Yup, OK, makes sense. Strange that OP would use == in one situation but === in another.
Hi. None of the solutions above seem to work. It just returns back all the data and no filters are applied. I dont really use == over === for any particular reason, I could make them the same. Mayb I can try REGEX as an option for this? Not sure what other route to take other then multiple if conditions which I really would like to avoid...
Can you update the question with some actual data and the expected result?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.