开发者

jQuery Access Object Value

Hi guys can you please show me how to access the content inside the audioCollection object?

I am using the Echonest jQuery plugin with jQuery Templates

https://github.com/Rodeoclash/Echonest-jQuery-Plugin/blob/master/scripts/echonest.js

I went on firebug and typed console.log(audioCollection) and I am getting ReferenceError: audioCollection is not defined. Not sure if I am doing it right.

echonest.artist(artist).audio( function(开发者_开发百科audioCollection) {

$('#blog-page').find('.post').append( audioCollection.to_html('<p class="song-url" style="display:none;">${url}</p>') ); 
//appends url variable to html inside of audioCollection

var testing = audioCollection;  //returns [Object object]
});

Thank you!


I'm Not familiar with the object, but you can try to use my dump() function to see what's in it..

echonest.artist(artist).audio( function(audioCollection) {

    $('#blog-page').find('.post').append( audioCollection.to_html('<p class="song-url" style="display:none;">${url}</p>') ); 
    //appends url variable to html inside of audioCollection

    var testing = audioCollection;  //returns [Object object]
    alert(dump(testing));        
});

function dump(arr,level) {
    var dumped_text = "";
    if(!level) level = 0;

    //The padding given at the beginning of the line.
    var level_padding = "";
    for(var j=0;j<level+1;j++) level_padding += "    ";

    if(typeof(arr) == 'object') { //Array/Hashes/Objects 
        for(var item in arr) {
            var value = arr[item];

            if(typeof(value) == 'object') { //If it is an array,
                dumped_text += level_padding + "'" + item + "' ...\n";
                dumped_text += dump(value,level+1);
            } else {
                dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
            }
        }
    } else { //Stings/Chars/Numbers etc.
        dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
    }
    return dumped_text;
}

UPDATE

to access your values, you can do something like this

var songs = testing.audio;
for (var x=0; x<songs.length; x++){
    alert(songs[x].title);
}


I you only want a raw view of the object, I'd suggest the widely used JSON.stringify():

echonest.artist(artist).audio(function(audioCollection) {
    console.log(JSON.stringify(audioCollection));

    // Appends url variable to html inside of audioCollection.
    $('#blog-page').find('.post').append(audioCollection.to_html(
        '<p class="song-url" style="display:none;">${url}</p>')); 
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜