I get an array of objects with image locations via API and I can't manage to render it.
I managed to get my array into chuncks of 4 using underscore, but I can't get my head around how to massage my data into correct form so I could render it out through mustache.
The code currently looks like this:
var template = "<ul>{{#images}}<li>{{location}}</li>{{/images}}</ul>";
var data = [
{"location":"gallery\/Baar_Toit_5.jpg"},
{"location":"gallery\/Baar_Toit_7.jpg"},
{"location":"gallery\/Baar_Toit_8.jpg"},
{"location":"gallery\/Baar_Int_1.jpg"},
{"location":"gallery\/Baar_Int_2.jpg"},
{"location":"gallery\/Baar_Int_3.jpg"},
{"location":"gallery\/Baar_Int_4.jpg"},
{"location":"gallery\/Baar_Uus_01.jpg"},
{"location":"gallery\/Baar_Uus_02.jpg"},
{"location":"gallery\/Baar_Uus_03.jpg"},
{"location":"gallery\/Baar_Uus_04.jpg"},
{"location":"gallery\/Baar_Uus_05.jpg"},
{"location":"gallery\/Baar_Uus_06.jpg"},
{"location":"gallery\/Baar_Uus_07.jpg"}
];
var n = 4;
var imgs = _.groupBy(data, function(element, index){
return Math.floor(index/n);
});
console.log(imgs);
Mustache.parse(template);
var rendered = Mustache.render(template, { imgs: JSON.parse(imgs) });
$('#gallery').html(rendered);
I created a small sandbox for testing purposes, feel free to play around with it: http://jsfiddle.net/qK5NT/149/
My desired output is:
<ul>
<li>
<p>img/1.jpg</p>
<p>img/2.jpg</p>
<p>img/3.jpg</p>
<p>img/4.jpg</p>
</li>
<li>
<p>img/5.jpg</p>
<p>img/6.jpg</p>
<p>img/5.jpg</p>
<p>img/6.jpg</p>
</li>
<li>
<p>img/5.jpg</p>
<p>img/6.jpg</p>
<p>img/5.jpg</p>
<p>img/6.jpg</p>
</li>
<li>
<p>img/5.jpg</p>
<p>img/6.jpg</p>
</li>
</ul>
Any help will be appreciated!