0

First of all thanks for the coments to make the post understable. I'm from argentina and evidently my english is not good enough.

I'm being trained as a junior developer and my task is to filter by party a json array within senators of the goverment 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 thought that you wont need the json code, but i got it wrong. Here is an example of the json code with the array inside it.

JSON ARRAY:

` var data = { "status":"OK", "copyright":" Copyright (c) 2019 Pro Publica Inc. All Rights Reserved.", "results":[ { "congress": "116", "chamber": "Senate",

     "num_results": 100,
     "offset": 0,
     "members": [
          {
             "id": "A000360",
             "title": "Senator, 2nd Class",
             "short_title": "Sen.",
             "api_uri":"https://api.propublica.org/congress/v1/members/A000360.json",
             "first_name": "Lamar",
             "middle_name": null,
             "last_name": "Alexander",
             "suffix": null,
             "date_of_birth": "1940-07-03",
             "gender": "M",
             "party": "R",
             "leadership_role": null,
             "twitter_account": "SenAlexander",
             "facebook_account": "senatorlamaralexander",
             "youtube_account": "lamaralexander",
             "govtrack_id": "300002",
             "cspan_id": "5",
             "votesmart_id": "15691",
             "icpsr_id": "40304",
             "crp_id": "N00009888",
             "google_entity_id": "/m/01rbs3",
             "fec_candidate_id": "S2TN00058",
             "url": "https://www.alexander.senate.gov/public",
             "rss_url": "https://www.alexander.senate.gov/public/?a=RSS.Feed",
             "contact_form": "http://www.alexander.senate.gov/public/index.cfm?p=Email",
             "in_office": true,
             "cook_pvi": null,
             "dw_nominate": 0.324,
             "ideal_point": null,
             "seniority": "17",
             "next_election": "2020",
             "total_votes": 386,
             "missed_votes": 76,
             "total_present": 0,
             "last_updated": "2019-12-10 06:56:21 -0500",
             "ocd_id": "ocd-division/country:us/state:tn",
             "office": "455 Dirksen Senate Office Building",
             "phone": "202-224-4944",
             "fax": "202-228-3398",
             "state": "TN",
             "senate_class": "2",
             "state_rank": "senior",
             "lis_id": "S289"
             ,"missed_votes_pct": 19.69,
             "votes_with_party_pct": 97.09,
             "votes_against_party_pct": 2.91
           },
                       {
             "id": "B001230",
             "title": "Senator, 1st Class",
             "short_title": "Sen.",
             "api_uri":"https://api.propublica.org/congress/v1/members/B001230.json",
             "first_name": "Tammy",
             "middle_name": null,
             "last_name": "Baldwin",
             "suffix": null,
             "date_of_birth": "1962-02-11",
             "gender": "F",
             "party": "D",
             "leadership_role": null,
             "twitter_account": "SenatorBaldwin",
             "facebook_account": "senatortammybaldwin",
             "youtube_account": "witammybaldwin",
             "govtrack_id": "400013",
             "cspan_id": "57884",
             "votesmart_id": "3470",
             "icpsr_id": "29940",
             "crp_id": "N00004367",
             "google_entity_id": "/m/024v02",
             "fec_candidate_id": "H8WI00018",
             "url": "https://www.baldwin.senate.gov",
             "rss_url": "https://www.baldwin.senate.gov/rss/feeds/?type=all",
             "contact_form": "https://www.baldwin.senate.gov/feedback",
             "in_office": true,
             "cook_pvi": null,
             "dw_nominate": -0.502,
             "ideal_point": null,
             "seniority": "7",
             "next_election": "2024",
             "total_votes": 386,
             "missed_votes": 1,
             "total_present": 1,
             "last_updated": "2019-12-10 06:56:22 -0500",
             "ocd_id": "ocd-division/country:us/state:wi",
             "office": "709 Hart Senate Office Building",
             "phone": "202-224-5653",
             "fax": null,
             "state": "WI",
             "senate_class": "1",
             "state_rank": "junior",
             "lis_id": "S354"
             ,"missed_votes_pct": 0.26,
             "votes_with_party_pct": 93.83,
             "votes_against_party_pct": 6.17
           },
                       {
             "id": "B001261",
             "title": "Senator, 1st Class",
             "short_title": "Sen.",
             "api_uri":"https://api.propublica.org/congress/v1/members/B001261.json",
             "first_name": "John",
             "middle_name": null,
             "last_name": "Barrasso",
             "suffix": null,
             "date_of_birth": "1952-07-21",
             "gender": "M",
             "party": "R",
             "leadership_role": "Senate Republican Conference Chair",
             "twitter_account": "SenJohnBarrasso",
             "facebook_account": "johnbarrasso",
             "youtube_account": "barrassowyo",
             "govtrack_id": "412251",
             "cspan_id": "1024777",
             "votesmart_id": "52662",
             "icpsr_id": "40707",
             "crp_id": "N00006236",
             "google_entity_id": "/m/02rsm32",
             "fec_candidate_id": "S6WY00068",
             "url": "https://www.barrasso.senate.gov",
             "rss_url": "https://www.barrasso.senate.gov/public/?a=rss.feed",
             "contact_form": "https://www.barrasso.senate.gov/public/index.cfm/contact-form",
             "in_office": true,
             "cook_pvi": null,
             "dw_nominate": 0.538,
             "ideal_point": null,
             "seniority": "13",
             "next_election": "2024",
             "total_votes": 386,
             "missed_votes": 0,
             "total_present": 0,
             "last_updated": "2019-12-09 19:32:09 -0500",
             "ocd_id": "ocd-division/country:us/state:wy",
             "office": "307 Dirksen Senate Office Building",
             "phone": "202-224-6441",
             "fax": null,
             "state": "WY",
             "senate_class": "1",
             "state_rank": "junior",
             "lis_id": "S317"
             ,"missed_votes_pct": 0.00,
             "votes_with_party_pct": 96.88,
             "votes_against_party_pct": 3.13
           },`

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

1
  • 2
    Please rephrase your question. What are you trying to filter and what is the thing to filter against? If possible, create a Stackblitz or something similar Commented Dec 18, 2019 at 13:24

1 Answer 1

1

You can use the array of checked values within your filter like so:

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

This will return an array containing only the objects from the miembros array which have a party attribute that matches one of the checked values.

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

2 Comments

I also think that this is what he wants. The const checked contains the values of the checkboxes. His data array (called miembros?) has be to be filtered against it, which means the include. Do note that you have to return miembros.filter(...) as filter creates a new array
That's right. The code that James gave to me is ok, but i need to make it interactive, filtering the json array at the moment with the checkbox value, the result should be displayed in the table that i created with html (done)

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.