Search code examples
javascriptjsrender

Accessing another array by index


My data are in the form:

data = { arr_id:[0,2,5], arr_description:["description 0","description 2","description 5"]}

My template

<script id="tmp" type="text/x-jsrender">
<ul>
        {{for arr_id}}
            <li>id: {{>}} **I NEED THIS->** {{:agg_desc[#Index]}} </li>
        {{/for}}
</ul>
</script>

The arrays arr_id arr_description maps same data (the one with an ID the latter with a description) I want to iterate over the first one and use the #index to access the second one.


Solution

  • You need to pass in the arr_description array to the inner context.

    Here are some relevant links:

    For example, you can do this:

    {{for arr_id ~arr_desc=arr_description}}
      <li>id: {{>}}  {{:~arr_desc[#index]}} </li>
    {{/for}}
    

    or this:

    {{for arr_id}}
      <li>id: {{>}}  {{:~root.arr_description[#index]}} </li>
    {{/for}}