mercredi 18 décembre 2019

Collecting data from an Json array and filter with checkboxes

I'm being trained as a junior developer and my task is to filter a json array with three checkboxes. At the moment I have my HTML code with the checkboxes, they have onchange property, then in my javascript code I used

Array.from(document.querySelectorAll('input[name=party]:checked')).map(elt => elt.value)

to take the array with the value of the checkboxes, but I have no idea how to compare the checkbox value with the json array to filter what I need.

Here we've got the code at the moment

HTML:

<div class='row'> <b>Filter by Party:</b> <label for='Republican'>Republican</label> <input onchange="partyFilter" type='checkbox' name='party' id='Republican' value='R'> <label for='Democrat'>Democrat</label> <input onchange="partyFilter" type='checkbox' name='party' id='Democrat' value='D'> <label for='Independent'>Independent</label> <input onchange="partyFilter" type='checkbox' name='party' id='Independent' value='I'> </div>

JAVASCRIPT (taking checkboxes values)

`Array.from(document.querySelectorAll('input[name=party]:checked')).map(elt => elt.value)

JAVASCRIPT (filtering my array to take the "party" value miembros.filter(rep => rep.party === 'R') miembros.filter(dem => rep.party === 'D') miembros.filter(ind => rep.party === 'I')`

I've no idea how to connect the values received with mi javascript function to filter the array.




Aucun commentaire:

Enregistrer un commentaire