Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
filling select input using the value from a map where the key is from another select input

Short question: I have a list of countries, and a dropdown menu where people can select multiple countries. I have a map where each key of the map is a country name, and each value of the country is a city. I want to put in options the cities from the countries selected, dynamically.

How to do this ?

<script>
    $: countries_chosen = []
    $: cities_chosen = []
    let countries = ["france", "spain", "germany"]
    let cities = []
    let all_cities = {
        "france": ["paris", "bordeau"],
        "spain": ["barcelona", "madrid"],
        "germany": ["berlin", "frankfurt"]
    }
    $: console.log(countries_chosen)
    $: console.log(cities_chosen)
</script>

 <h1>
    countries
</h1>


    <label>
        <select multiple  bind:value={countries_chosen}>
            {#each countries as country}
            <option value={country}>
                {country}
            </option>
            {/each}
    </label>


<h1>
    cities
</h1>

    <label>
        <select multiple bind:value={cities_chosen}>
            {#each cities as city}
            <option value={city}>
                        {city}
            </option>
            {/each}
    </label>

REPL: https://svelte.dev/repl/61c93b05a2374d1197fb3a38e86b75a1?version=3.46.4

Thanks !

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

here is the working code:

<script>
    $: countries_chosen = []
    $: cities_chosen = []
    let countries = ["france", "spain", "germany"]
    let cities = []
    let all_cities = {
        "france": ["paris", "bordeau"],
        "spain": ["barcelona", "madrid"],
        "germany": ["berlin", "frankfurt"]
    }
    $: console.log(countries_chosen)
    $: console.log(cities_chosen)
</script>

 <h1>
    countries
</h1>


    <label>
        <select multiple  bind:value={countries_chosen}>
            {#each countries as country}
            <option value={country}>
                {country}
            </option>
            {/each}
    </label>


<h1>
    cities
</h1>

    <label>
        <select multiple bind:value={cities_chosen}>
            {#each countries_chosen as country}
                {#each all_cities[country] as city}
                                <option value={city}>
                                    {city}
                            </option>
              {/each}

            {/each}
    </label>

about 4 years ago · Juan Pablo Isaza Report

0

You would reactively fill up the cities array based on countries_chosen by simply mapping each of the selected countries to their respective cities and then flattening this array.

    $: cities = countries_chosen.map(c => all_cities[c]).flat()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!