簡體   English   中英

如何遍歷並從復雜對象(數組)中提取屬性值?

[英]How to loop through and extract property values from a complex object (Array)?

以下代碼片段來自用JQuery編寫的開源撲克客戶端。 它顯示以前由撲克服務器生成的撲克表。 加載頁面后,該表將顯示在頁面上的div中。

//
// featured table
//
jpoker.plugins.featuredTable = function(url, options) {

    var opts = $.extend({}, jpoker.plugins.featuredTable.defaults, options);
    var server = jpoker.url2server({ url: url });

server.registerUpdate(function(server, what, packet) {
    if (packet && packet.type == 'PacketPokerTableList') {
        if (packet.packets.length === 0) {
        var updated = function(server, what, packet) {
            if(packet && packet.type == 'PacketPokerTableList') {
            var found = null;
            for(var i = packet.packets.length - 1; i >= 0 ; i--) {
                var subpacket = packet.packets[i];
                if(opts.compare(found, subpacket) >= 0) {
                found = subpacket;
                }
            }
            if(found) {
                found.game_id = found.id;
                server.setTimeout(function() { server.tableJoin(found.game_id); }, 1);
            }
            return false;
            } else {
            return true;
            }
        };
        server.registerUpdate(updated, null, 'featuredTable ' + url);
        server.selectTables(opts.string);
        }
        return false;
    } else {
        return true;
    }
    }, null, 'featuredTable ' + url);
    server.selectTables('my');
    return this;
};

jpoker.plugins.featuredTable.defaults = {
    string: '',
    compare: function(a, b) { return a && b && b.players - a.players; }
};

該代碼引用了以下復雜的動態對象。

{"players":3,"type":"PacketPokerTableList","packets":[{"observers":1,"name":"sitngo417","waiting":0,"percent_flop":0,"average_pot":10852,"skin":"default","variant":"holdem","hands_per_hour":120,"betting_structure":"level-15-30-no-limit","currency_serial":0,"muck_timeout":5,"players":2,"reason":"TableList","tourney_serial":58151,"seats":2,"player_timeout":60,"type":"PacketPokerTable","id":97},{"observers":0,"name":"sitngo418","waiting":0,"percent_flop":100,"average_pot":97700,"skin":"default","variant":"holdem","hands_per_hour":100,"betting_structure":"level-15-30-no-limit","currency_serial":0,"muck_timeout":5,"players":1,"reason":"TableList","tourney_serial":58151,"seats":2,"player_timeout":60,"type":"PacketPokerTable","id":98}],"tables":2,"time__":1329073257148}

本質上,如果您願意的話,對象中有嵌套的對象或數組中的數組。 主要對象被命名為“ packet”,類型為“ PacketPokerTableList”,嵌套對象被命名為“ packets”,每個子包中的子包的類型為“ PacketPokerTable”。 數據包中可以有任意數量的子數據包,它們取決於給定錦標賽中的表數。 類型為“ PacketPokerTable”的每個子包都包含許多具有給定值的元素,這些值代表錦標賽中的每個表。 上面代碼中的for循環僅查看數據包中的第一個子數據包,並檢索“ id”的值(在這種情況下為97),然后通過調用server.tableJoin()顯示表。

我希望修改此默認行為,以便“ for循環”在數據包類型“ PacketPokerTableList”內的所有“ PacketPokerTable”類型的子數據包之間循環,並從每個子數據包中檢索“ id”的值。 然后,而不是自動在當前頁面的div中顯示表格; 我希望對於每個表“ id”,使用window.open()方法在新的瀏覽器窗口中顯示其相關表。

我無法克服的第一個問題是如何遍歷此復雜對象並檢索“ id”的所有值。 不知道是否可能。 我在網絡上研究的所有內容大多與具有基本數組的“ for循環”有關。 其中沒有一個有幫助。 第二個障礙是如何將此變量與在子窗口中執行的函數一起傳遞。

看來自己解決這個問題是我的職責所在,我非常感謝您提供有關如何完成此任務的任何意見。

要遍歷每個元素,必須使用遞歸。 我不太了解您要做什么,但是這里有一個簡單的示例:

function loop(obj) {
  if(obj.someprop == 'someval') {
    //do something
  } else {
    loop(obj);
  }
}

我不完全知道server.tableJoin()的工作方式,但是如果它返回表的html,那么就可以解決問題。 如果沒有,您將需要做的所有事情來為新表創建html並將其放在適當的位置。

//make sure the object has the necessary info       
if(packet && packet.type == 'PacketPokerTableList' && packet.packets && packet.packets.length > 0) {
        //go through each packet.packets
        for (var i=0;i < packet.packets.length;i++){
            if(packet.packets[i].type == 'PacketPokerTable'){
                var id = packet.packets[i].id;
                //open window
                var newWin = window.open('_blank',id);
                //write new content in the new window
                newWin.document.write(server.tableJoin(id));
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM