简体   繁体   English

如何遍历并从复杂对象(数组)中提取属性值?

[英]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. 以下代码片段来自用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. 加载页面后,该表将显示在页面上的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; }
};

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". 主要对象被命名为“ packet”,类型为“ PacketPokerTableList”,嵌套对象被命名为“ packets”,每个子包中的子包的类型为“ 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. 类型为“ PacketPokerTable”的每个子包都包含许多具有给定值的元素,这些值代表锦标赛中的每个表。 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(). 上面代码中的for循环仅查看数据包中的第一个子数据包,并检索“ id”的值(在这种情况下为97),然后通过调用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. 我希望修改此默认行为,以便“ for循环”在数据包类型“ PacketPokerTableList”内的所有“ PacketPokerTable”类型的子数据包之间循环,并从每个子数据包中检索“ id”的值。 Then instead of automatically displaying the table in a div of the current page; 然后,而不是自动在当前页面的div中显示表格; I want, for each table "id", to display its related table in a new browser window with the window.open() method. 我希望对于每个表“ id”,使用window.open()方法在新的浏览器窗口中显示其相关表。

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". 我无法克服的第一个问题是如何遍历此复杂对象并检索“ 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; 我在网络上研究的所有内容大多与具有基本数组的“ for循环”有关。 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. 看来自己解决这个问题是我的职责所在,我非常感谢您提供有关如何完成此任务的任何意见。

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

I'm not entirely sure how server.tableJoin() works, but if it returns html for the table, then this will do the trick. 我不完全知道server.tableJoin()的工作方式,但是如果它返回表的html,那么就可以解决问题。 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. 如果没有,您将需要做的所有事情来为新表创建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.

相关问题 如何遍历复杂的 object 或 object 数组 - How to loop through complex object or array of object 如何将对象的属性值提取到数组中并使数组中的元素不同? - How to extract the property values of a object into an array and make elements in the array different? 如何将 JavaScript object 的属性值提取到数组中? - How might I extract the property values of a JavaScript object into an array? 如何在 JS 中循环遍历复杂的 object - How to loop through a complex object in JS 异步编程 - 如何遍历对象数组并使用从 api 获取的数据更改每个对象的属性? - async programming - how to loop through an array of objects and change a property to each object with data fetched from an api? 如何从 object 中提取具有 typescript 中的属性和数组的数组? - How to extract an array from an object with property and array in typescript? jQuery,如何遍历复杂数组 - jquery, how to loop through complex array 循环通过 object 并返回特定属性的值 - Loop through an object and return the values of a specific property 如何将属性与对象数组中的值进行比较? - How to compare a property to the values from an array of object? Javascript-遍历具有数组属性的对象 - Javascript - Loop through object with array property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM