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
Setting first (nth?) dropdown entry as default using array values

I have a dropdown that works well with simple values, but I just tried using arrays as values and now the initial dropdown-entry is empty.

I have created a REPL to illustrate the problem. As you can see I have also tried using selected=... to automatically select the first entry, but it doesn't work.

The only thing I can think of is to introduce another property to go back to simple values and then extract the array by filtering the original array, but it seems rather cumbersome. I wonder if there is another, more elegant, way that I'm missing.

Thank you in advance!

Edit: Here the code example as suggested:

<script>
    const availableFilters = [
        { key: ['filterAll'], display: 'All Columns' },
        { key: ['name1', 'name2' ], display: 'Entry Names' },
        { key: ['ID1', 'ID2' ], display: 'Entry IDs' },
        { key: ['name1'], display: 'Entry 1 Name' },
        { key: ['ID1'], display: 'Entry 1 ID' },
        { key: ['name2'], display: 'Entry 2 Name' },
        { key: ['ID2'], display: 'Entry 2 ID' },
    ];
    
    const simpleAvailableFilters = [
        { key: 'filterAll', display: 'All Columns' },
        { key: 'name1', display: 'Entry 1 Name' },
        { key: 'ID1', display: 'Entry 1 ID' },
        { key: 'name2', display: 'Entry 2 Name' },
        { key: 'ID2', display: 'Entry 2 ID' },
    ];
    
    
    let currentFilter = { key: ['filterAll'], value: "", code: 0 };
    let simpleCurrentFilter = { key: 'filterAll', value: "", code: 0 };
</script>

Dropdown with array values: 
<select bind:value={currentFilter.key}>
    {#each availableFilters as filter, i}
        <option selected={i === 0 ? 'selected' : ''} value={filter.key}>{filter.display} - {i}</option>
    {/each}
</select>
<span>
    Selected key: {currentFilter.key}
</span>
<br/>
Simple Dropdown:
<select bind:value={simpleCurrentFilter.key}>
    {#each simpleAvailableFilters as filter, i}
        <option value={filter.key}>{filter.display} - {i}</option>
    {/each}
</select>
<span>
    Selected key: {simpleCurrentFilter.key}
</span>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try this:

let currentFilter = availableFilters[0].key;

and you can remove selected={i === 0 ? 'selected' : ''} from the option.

See https://svelte.dev/tutorial/select-bindings

about 4 years ago · Juan Pablo Isaza Report

0

Simply replace

let currentFilter = { key: ['filterAll'], value: "", code: 0 };

by

let currentFilter = availableFilters[0];

Even though the object has the exact same structure, it's still a different object that the one on the first position in the array. {} === {} // false The same applies to arrays.

['filterAll'] === ['filterAll'] // false
'filterAll' === 'filterAll' // true

currentFilter would have then this structure

{key: *bindedValue*, display: 'All Columns'}

If you want to extract the array you have to access the key when setting the initial value

let currentFilter = availableFilters[0].key;

and change the binding on the option

<select bind:value={currentFilter}>

REPL A Repl is great to have, but it would be better to include the code as well, in case the Repl might break...

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!