Svelte: Addition to tutorial: Calling a function in an "each" block

Created on 23 Oct 2019  路  2Comments  路  Source: sveltejs/svelte

The tutorial/documentation should have an example of calling a function inside an each block to do some additional processing. This is non-intuitive so putting it in the tutorial would be very helpful.

e.g.

<script>
function dosomething(item) {
  var dict = {};

   // Put original object into the dictionary
    dict.item = item;

  // Do some processing and put various results in the dictionary to be used in the loop.
  dict.result = "success";
  dict.count = 12;

  return dict;
}
</script>


{#each list as item}
  {#each [dosomething(item)] as val}
   <div>{val.result} {val.item} {val.count}</div>
  {/each}
{/each}
each tutorial

Most helpful comment

Is there a reason to do it that way instead of just

{#each list.map(dosomething) as val}
  <div>{val.result} {val.item} {val.count}</div>
{/each}

Here's a REPL example of this style: https://svelte.dev/repl/3d071e514c7641c580980c48dd1b6665?version=3.14.0

All 2 comments

Is there a reason to do it that way instead of just

{#each list.map(dosomething) as val}
  <div>{val.result} {val.item} {val.count}</div>
{/each}

Here's a REPL example of this style: https://svelte.dev/repl/3d071e514c7641c580980c48dd1b6665?version=3.14.0

Sure but it's not in the TUTORIAL.

Was this page helpful?
0 / 5 - 0 ratings