[英]Output a large nested array with both sub-arrays and number objects
I am keeping track of each round of hands/results dealt in a blackjack game.我正在跟踪二十一点游戏中处理的每一轮手牌/结果。 I need to output each "round" as a single line.
我需要将每个“回合”输出为一行。 There are 'x' number of rounds.
有“x”轮。 I am storing the results for each round in a big array.
我将每一轮的结果存储在一个大数组中。 My problem is I have 2 arrays within my big array and 2 numeric.
我的问题是我的大数组中有 2 个数组和 2 个数字。 I need to output each of the 4 'objects' per line, as round results.
我需要将每行 4 个“对象”中的每一个输出为圆形结果。 I cannot figure out how to 'parse' the non-numeric array objects mixed with the numeric objects.
我无法弄清楚如何“解析”与数字对象混合的非数字数组对象。 Should I just create two arrays, one object and one numeric and output them separately?
我应该只创建两个数组,一个对象和一个数字并分别输出吗? Output goal is 1st line: 3,2,9,6;10,2,6;3,10 and so on for each round, one line.
输出目标是第一行:3,2,9,6;10,2,6;3,10 以此类推,每一轮,一行。
<script>
var ph = [3,2,9,6]; var dh = [10,2,6]; var seq = 3, var bet = 10; var result;
allhands.push(ph);allhands.push(dh);allhands.push(seq);allhands.push(bet)
//allhands looks like this:[[3,2,9,6], [10,2,6], 3,10]
function showallhands(){
for (e = 0;e<allhands.length;e++){
for (f = 0; allhands[e][f];f ++){
result += allhands[e] + allhands[e][f]+";"+"<br >" ;
}
}
return result;
}
</script>
result = showallhands();
document.getElementById("ALLhands").innerHTML = result;
One approach is to create a game object then store the hands in it:一种方法是创建一个游戏对象,然后将手放在其中:
function Game() {
this.ph = [3, 2, 9, 6];
this.dh = [10, 2, 6];
this.seq = 3;
this.bet = 10;
this.getHands = function () {
return this.ph + ';' + this.dh + ';' + this.seq + ';' + this.bet + ';<br>';
};
};
var games = [];
games.push(new Game());
games.push(new Game());
games.push(new Game());
var result = "";
function showallhands() {
for (e = 0; e < games.length; e++) {
result += games[e].getHands();
}
return result;
}
result = showallhands();
document.getElementById("ALLhands").innerHTML = result;
If you want to keep the array and not use OOP, I think this would do the trick:如果您想保留数组而不使用 OOP,我认为这可以解决问题:
var arr = [
[3, 2, 9, 6],
[10, 2, 6], 3, 10, "hello"
];
arr.forEach(function each(item) {
if (Array.isArray(item)) {
item.forEach(each);
} else {
console.log(item);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.