Sublize Sublize - 4 months ago 7
JSON Question

Showing formatted JavaScript as content in HTML

Hi I am new to JavaScript and I need to show this JavaScript object (pseudo-JSON) into a div on a HTML page, I can't use jQuery can anyone help please.

var data = {
"music": [
{
"id": 1,
"artist": "Oasis",
"album": "Definitely Maybe",
"year": "1997"
},
]
}

Answer

Not sure which data you want to show in the div. data is an object while music is a key to data. data.music will return the music array. Then use forEach array method to loop through the array.

Hope this snippet will be useful

var data = {
    "music": [
        {
        "id": 1,
        "artist": "Oasis",
        "album": "Definitely Maybe",
        "year": "1997"
        },
    ]
}

var _getData = data.music // will return music array;
_getData.forEach(function(item){
document.write('<pre>'+item.id+'</pre>')
})

JSFIDDLE

Comments