0

I'm looping through object keys in a firebase database then checking the property myFirebaseObject.category for a specific category. I have multiple filters that will be stacked. If a checkbox is checked I want to include all objects associated with that category and combine them with other selected categories in my new array.

I'm not necessarily looking for code unless it's the best way to explain your approach. I'm trying to learn so I'm thinking more conceptually: "What is the best approach to building stackable filters using checkboxes in javascript"

Is there a good way to dynamically build this if statement(assuming 1 and 3 were checked)?

If statement:

if (myFirebaseObject.category === '1' || myFirebaseObject.category === '3' && myFirebaseObject.enabled === '1'){//populate an array}; 

My checkboxes:

<input id="cat1" class="category_check" type="checkbox" checked="true" name="filter" value="1" >1<br> <input id="cat2" class="category_check" type="checkbox" name="filter" value="2">2<br> <input id="cat3" class="category_check" type="checkbox" checked="true" name="filter" value="3">3<br> <input id="cat4" class="category_check" type="checkbox" name="filter" value="4">4<br> 

1 Answer 1

1

Instead of dynamic IF statement, use an array. You can then check to see if the item is in the array easily.

See: JavaScript is in array

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

1 Comment

Thanks, I probably wouldn't have thought of that but now that you've pointed it out it makes perfect sense.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.