1

I'm reading a JSON file which contains information about a number of users.

{
"SRM": [{
    "title": "Firstname Surname",
    "image": "firstname",
    "subtitle":"the subtitle"     
}, {
    "title": "Firstname Surname",
    "image": "firstname",
    "subtitle":"the subtitle" 
}, {
    "title": "Firstname Surname",
    "image": "firstname",
    "subtitle":"the subtitle" 
}]

}

The first part is working fine, in getting their details and building my HTML like so.

        $.getJSON("srm.json", function(data) {
        var i = 1;
        $.each(data.SRM, function(key, val) {

                 $("#image"+i).attr("src", "custom/img/who/"+val.image+".jpg");
                 $("#bioimage"+i).attr("href", "custom/img/who/"+val.image+"b.jpg");
                  $("#title"+i).html(val.title);
                   $("#subtitle"+i).html(val.subtitle);
                 i = i+1;

        });


    });

However I have now introduced tags, which will act as filters.

{
"SRM": [{
    "title": "CHETNA SHARMA",
    "image": "chetna",
    "subtitle":"SERVICE RELATIONSHIP MANAGER",
    "tags":["1","2","3"]
}]

}

I can't figure out how to correctly read these back so that I can add them all to the class of a user.

for example the above tags should add them like so

    <div id="thetags" class="cbp-item 1 2 3">

any pointers on how best to read out the whole tags array correctly? thanks

Ray_Hack
  • 973
  • 2
  • 9
  • 27
  • Possible duplicate of [Jquery : Convert javascript array to string](http://stackoverflow.com/questions/5289403/jquery-convert-javascript-array-to-string) – Mike Scotty Sep 12 '16 at 09:19

1 Answers1

4
var tags = val.tags;
tags.join(" ");  // this will convert array to string.
Ish
  • 2,085
  • 3
  • 21
  • 38