jQuery访问对象值

时间:2011-02-28 21:16:43

标签: jquery

大家好,请您告诉我如何访问audioCollection对象中的内容?

我正在使用带有jQuery模板的Echonest jQuery插件

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

我继续使用萤火虫并键入console.log(audioCollection),我得到了ReferenceError: audioCollection is not defined。不确定我是否做得对。

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]
});

谢谢!

2 个答案:

答案 0 :(得分:2)

我不熟悉该对象,但您可以尝试使用我的dump()函数来查看其中的内容。

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;
}

<强>更新

要访问您的值,您可以执行类似的操作

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

答案 1 :(得分:1)

我只想要一个对象的原始视图,我建议广泛使用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>')); 
});