Skip to main content

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



The following snippet is from an opensource poker client written in JQuery. It displays a poker table that has been previously spawned by the poker server. The table is displayed within a div on the page after the page is loaded.







//

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

};







The code references the following complex dynamic object.







{"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}







Essentially, there are nested objects within an object or arrays within arrays if you prefer. The main object is named "packet" with a type of "PacketPokerTableList" and the nested objects are named "packets" with each sub packet within packets having a type of "PacketPokerTable". There can be any number of sub packets within packets and they vary depending on the number of tables that are in a given tournament. Each subpacket of type "PacketPokerTable" contains a number of elements that have a given value that is representative for each table in the tournament. The for loop in the above code only looks at the first subpacket in packets and retrieves the value of "id", which in this case is 97 and then displays the table by calling server.tableJoin().





I wish to modify this default behaviour so that the "for loop" cycles through all of the sub packets of type "PacketPokerTable" within packet type "PacketPokerTableList" and retrieve the value of "id" from each of the sub packets. Then instead of automatically displaying the table in a div of the current page; I want, for each table "id", to display its related table in a new browser window with the window.open() method.





The first obstical that I haven't been able to overcome is how to loop through the this complex object and retrieve all the values of "id". Not sure if it is possible or not. Everything that I have researched on the web mostly relate to "for loops" with very basic arrays; of which none have been helpfull. The second obstacle is how to pass this variable along with a function that executes in the child windows.





It seems that solving this own my own is way above my station, I would greatly appreciate any input on how to accomplish this task.


Comments

  1. To loop through each element you must use recursion. I didn't really understood what you want to do, but here is a simple example:

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

    ReplyDelete
  2. I'm not entirely sure how server.tableJoin() works, but if it returns html for the table, then this will do the trick. If not, you'll need to do whatever you do to create the html for the new table and put it in it's place.

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

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex