I have a data array that I want to filter based on if set variables are set to true or false. I want to write my code in a single function using operators (if possible) instead of having to code for every combination. My data sample looks like this:
chardata = [
{
"CharName": "Char1",
"Magic": TRUE,
"Melee": TRUE,
"Ranged": TRUE
},{
"CharName": "Char2",
"Magic": TRUE,
"Melee": FALSE,
"Ranged": FALSE
},{
"CharName": "Char3",
"Magic": FALSE,
"Melee": FALSE,
"Ranged": TRUE
}
]
I would like the filter to return Char1 whenever Magic and Ranged are set to true, but not return Char2 or Char3.
const filter = chardata.filter(function (ef) {
return Magic === true ?
ef["Magic"] === true : "",
Ranged === true ?
ef["Melee"] === true : "",
Melee === true ?
ef["Ranged"] === true : ""})
Is is possible to make this work using this method or are are more than one functions needed?
Thanks