Ray_Hack
Ray_Hack

Reputation: 981

JSON array to concatenated string

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

Upvotes: 1

Views: 120

Answers (1)

Ish
Ish

Reputation: 2105

var tags = val.tags;
tags.join(" ");  // this will convert array to string.

Upvotes: 4

Related Questions