I have some json objects, and some of them have some other objects inside them.
if I leave only json objs that do not have another obj inside them, and then apply the template, everything goes well, I get 3 li elements in this case.
but if I take the original json obj, the results will be slightly related. I believe that I need to do each statement to iterate through each sub json obj from within each main.
Maybe I'm a little confused, so here is some code.
I have some json data:
{ "msg_id":"134", "message":"Nick", "comment":[ { "com_id":"9", "comment":"test", }, { "com_id":"10", "comment":"testtt", }, { "com_id":"11", "comment":"testtttt", }] }, { "msg_id":"134", "message":"Nick", }, { "msg_id":"134", "message":"Nick", }
and I'm trying to create something like this: Nick
Test
testtt
testtttt
Nick Nick
I created a template like this:
function messagesTamplate(data) { $.each(data, function(index, obj) { msg += template.replace( /{{message}}/ig , obj.message ); if(obj.comment) { $.each(obj.comment, function(key, val) { msg += template.replace( /{{comment}}/ig , val.comment ); }); } }); return msg; }
then I just add this to the main ul.
thanks
json javascript jquery each mustache
Patrioticcow
source share