0

I have the following data in a json file. I want this data to be shown in dropdown lists, as: In first drop down, I can select the country. In second drop down, I can select from the states of the selected country. Then, the population of the place is shown in text below on the html page.

[
  {
    "country": "USA",
    "state": "Texas",
    "population": "20M"
  },
  {
    "country": "USA",
    "state": "Alabama",
    "population": "4M"
  },
  {
    "country": "USA",
    "state": "California",
    "population": "33M"
  },
  {
    "country": "India",
    "state": "Maharashtra",
    "population": "112M"
  },
  {
    "country": "Japan",
    "state": "Tokyo",
    "population": "13M"
  },
  {
    "country": "India",
    "state": "Bihar",
    "population": "104M"
  },
  {
    "country": "USA",
    "state": "Florida",
    "population": "15M"
  },
  {
    "country": "India",
    "state": "Gujarat",
    "population": "60M"
  },
  {
    "country": "USA",
    "state": "Georgia",
    "population": "8M"
  },
  {
    "country": "Japan",
    "state": "Osaka",
    "population": "8M"
  },
  {
    "country": "Japan",
    "state": "Saitama",
    "population": "7M"
  },
  {
    "country": "India",
    "state": "Tamil Nadu",
    "population": "72M"
  }
]

It will be more helpful if the code is in javascript. Thank you:)

I have tried this:--

<body>
    <p>
<input type="button" style="margin:10px 0; "onclick = "populateSelect()" value = "Start" />
    </p>
    <select id="sel" onchange="show(this)">
        <option value="">-- Select --</option>
    </select>
</body>
<script>    

var ele = document.getElementById('sel');
          for (var i = 0; i < data.length; i++) {
              if(data[i]['country']!==undefined){
              ele.innerHTML = ele.innerHTML + '<option value="' + data[i]['country'] + '">' + data[i]['country'] + '</option>'; }

</script>
4
  • 1
    Stackoverflow is not a code writing service, kindly mention what u have tried, and where u are facing issues! Commented Jun 7, 2021 at 5:28
  • 1
    I'm new to this, I tried populating the country in first dropdown, but was not able to populate the states of the selected country in the other drop down. Have gone through many resources, but didn't get! Commented Jun 7, 2021 at 5:34
  • Add in the question what u have tried, are u using Node.js? Commented Jun 7, 2021 at 5:35
  • Yes, I'm working with nodejs. Commented Jun 7, 2021 at 5:47

1 Answer 1

1

Not sure how you want it but here is what I have done. I have used .map() for filtering the unique country and parse it to the country dropdown. Btw, I also used the filtering at the last part for getting the population result once you have the country and state selected.

Check it out .map() and .filter for more info

And here is the working JsFiddle

const data = [
    {
    "country": "USA",
    "state": "Texas",
    "population": "20M"
  },
  {
    "country": "USA",
    "state": "Alabama",
    "population": "4M"
  },
  {
    "country": "USA",
    "state": "California",
    "population": "33M"
  },
  {
    "country": "India",
    "state": "Maharashtra",
    "population": "112M"
  },
  {
    "country": "Japan",
    "state": "Tokyo",
    "population": "13M"
  },
  {
    "country": "India",
    "state": "Bihar",
    "population": "104M"
  },
  {
    "country": "USA",
    "state": "Florida",
    "population": "15M"
  },
  {
    "country": "India",
    "state": "Gujarat",
    "population": "60M"
  },
  {
    "country": "USA",
    "state": "Georgia",
    "population": "8M"
  },
  {
    "country": "Japan",
    "state": "Osaka",
    "population": "8M"
  },
  {
    "country": "Japan",
    "state": "Saitama",
    "population": "7M"
  },
  {
    "country": "India",
    "state": "Tamil Nadu",
    "population": "72M"
  }
];

$( () => {
    const countryArr= [... new Set(data.map(x => x.country))];
  
    countryArr.forEach((ele, index) => {
    $('#countryS').append(`<option value="${ele}">${ele}</option>`)
  });
  
  // After choosing country, showing the state
    $('#countryS').on('change', function(){
    // Reset the state array and drop down 
    var stateArr = [];
    $('#stateS').html(`<option value="">-- Select --</option>`);
    $('#showPopulation').val('');
    
    data.forEach((ele,idx) => {
        if(ele.country == this.value){
        stateArr.push(ele.state);
      }
    }) // End of data forEach function
    stateArr.forEach((ele, index) => {
      $('#stateS').append(`<option value="${ele}">${ele}</option>`)
    });
  });
  
  // After choosing the state, show the population
  $('#stateS').on('change', function(){
    let country         = $('#countryS').val();
    let state           = $('#stateS').val();
    let population  = data.filter(x => {
        return x.country == country && x.state == state;
    })[0]['population']
    
    //Parse the data to the input field
    $('#showPopulation').val(population);
  })
  
}) // End of onLoad function
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<body>
  <span>Country</span>
  <select id="countryS">
    <option value="">-- Select --</option>
  </select>
  
  <span>State</span>
  <select id="stateS">
    <option value="">-- Select --</option>
  </select>
  
  <input id="showPopulation" placeholder="Population" readonly/>
  
</body>

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

Comments

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.