Search code examples
javascriptarraysmeteormeteor-blaze

How can i iterate an array in javascript using blaze templates of meteor.js?


Hello everyone i'm trying to iterate an array in javascript using blaze templates of meteor.js.

When I set a static value into array, ex: 6, the filled of this work really fine but when I set 'i' var in array no work at all. like this:

......."titulo": "{{#each scraper}}{{publicaciones.[6].titulo_pub}}{{/each}}"}.....

But when i set a 'i' var no work

......."citas": '{{#each scraper}}{{publicaciones.[i].citas}}{{/each}}',......

.

for(var i = 0; i <= {{#each scraper}}{{c_titulos}}{{/each}}; i++) {
        sample_data[i] = {"citas": '{{#each scraper}}{{publicaciones.[i].citas}}{{/each}}',
                          "autores": '{{#each scraper}}{{publicaciones.[i].authors}}{{/each}}',
                          "titulo": "{{#each scraper}}{{publicaciones.[6].titulo_pub}}{{/each}}"}
        sample_data.fill(sample_data[i], i, i + 1);
      }

If run this code and calling this object with console.log, console shows me the below message:enter image description here

Like i use a static value like '6', all elements of this array had the same "titulo".

Please if you could help me i'll be so grateful. Thanks so much to all. Regards


Solution

  • Blaze handles the iteration and the scope of each iteration for you. e.g.

    {{#each scrape in scraper}}
      {{scrape.titulo}}
      {{scrape.citas}}
      {{scrape.autores}}
    {{/each}}
    

    c.f. http://blazejs.org/guide/spacebars.html#Each-in