How to print both Object key and value with Each block in Svelte? - Stack Overflow

PHOTO EMBED

Sun Jul 25 2021 12:17:04 GMT+0000 (Coordinated Universal Time)

Saved by @joannpav #svelte

<script>
    const sections = {
        "Title 1": "paragraph",
        "Title 2": "paragraph",
        "Title 3": "paragraph",
        "Title 4": "paragraph",
        "Title 5": "paragraph"
    }
    // Object.entries() converts an Object into an array of arrays, 
    // each sub array first index is the a key and the second index is a value
    // Object.entries({key: value, key:value}) => [[key, value], [key,value]]
</script>

{#each Object.entries(sections) as [title, paragraph]}
    <h1>{title}</h1>
    <p>{paragraph}</p>
{/each}
content_copyCOPY

https://stackoverflow.com/questions/64909382/how-to-print-both-object-key-and-value-with-each-block-in-svelte