I've been trying to loop through a mulitnested json object but everytime it displays undefined. I've wanted to display playcount and the name of the song. I plan on using this with a bar chart.
I tried this expecting ['playcount', 'name']
function getData(){
$("#output").html("<b>hi there</b>");
$.getJSON('https://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&artist=drake&api_key=22102f7d3a814de0736edf670bd2c771&format=json',function(result){
console.log(result);
let testarray = result[0]['album'];
let newdata = [];
for (let i = 0; i < result.length; i++) {
testarray= result[i]['album']
console.log(testarray)
let item = []
item[0] = testarray[i]['playcount']
item[1] = testarray[i]['name']
newdata[j] = item
console.log(newdata);
}
console.log(newdata)
})
}
Let's first take a look at the data you are working with:
You are gettin an object that has a property with a key called topalbums. Top albums has two properties; an array called album and an object called @attr.
From the looks of it, you want to access the objects inside of the album array, and more specifically name and playcount.
Given the data you are working with I assume this is what you would be looking for:
To achieve this you can alter your code in the following fashion:
Alternatively, if you don't want an array of objects but an array of arrays like this
[['playcount', 'name']...], you can alter the code above like this:Hope this helps!