Iterate through json array using javascript

By
Advertisement
Javascript Array:

var myArray = {"artists":[{
            "a1":"Adam Sandler",
            "a2":"Adam Lambert",
            "a3":"Avril Levine",
            "a4":"Backstreet Boys",
            "a5":"Blackstreet",
            "a6":"Black Eye Peas",
            "a7":"Cool and the Gang",
            "a8":"Chicago",
            "a9":"Charlie Manson"

        }],
        "songs":[{
            "s1":"Grow Old With You",
            "s2":"Whatdaya Want From Me",
            "s3":"Yea yea",
            "s4":"Quit Playing Games With My Heart",
            "s5":"No Digity",
            "s6":"Meet Me Half way",
            "s7":"Doo wa ditty",
            "s8":"Fight for your honor",
            "s9":"Charlies Song"
        }],
        "genre":[{
            "g1":"Pop",
            "g2":"Pop",
            "g3":"Alternative",
            "g4":"R & B",
            "g5":"R & B",
            "g6":"Hip-Hop",
            "g7":"Funk",
            "g8":"Soft Rock",
            "g9":"Rock"
        }]};


Javascript code to loop only songs:

$.each(myArray.songs, function (i, ob) {
    $.each(ob, function (ind, obj) {
        $('#result').append("key:" + ind + " value:" + obj+"<br/>")
    });
});

Html Div To Paste The output:

<div id="result">
</div>

Output:

key:s1 value:Grow Old With You
key:s2 value:Whatdaya Want From Me
key:s3 value:Yea yea
key:s4 value:Quit Playing Games With My Heart
key:s5 value:No Digity
key:s6 value:Meet Me Half way
key:s7 value:Doo wa ditty
key:s8 value:Fight for your honor
key:s9 value:Charlies Song 

0 comments:

Post a Comment

Online Casino