0

我目前设置了以下数组:

var TicketInfo =
    {
        t1: {
            1: [7, 12, 35,39,41, 43],
            2: [7, 15, 20,34,45, 48],
            3: [3, 7, 10, 13, 22, 43],
            4: [2, 4, 5,23,27, 33]
        },
        t2: {
            1: [10, 12, 17,44,48, 49],
            2: [13, 15, 17, 18, 32, 39],
            3: [16, 17, 20, 45, 48, 49],
            4: [6, 16, 18, 21, 32, 40]
        }
        }

我想要做的是遍历这些以带回下面的数组。

作为测试,我尝试过这样的事情:

for(t in TicketInfo["t1"])
{
    i++;
        Write(t.i);
}

但它显然没有按照我想要的方式工作。

有任何想法吗?

我希望能够输出像这样的数组[7, 12, 35,39,41, 43]

谢谢

4

5 回答 5

8
var TicketInfo =
{
    t1: {
        1: [7, 12, 35,39,41, 43],
        2: [7, 15, 20,34,45, 48],
        3: [3, 7, 10, 13, 22, 43],
        4: [2, 4, 5,23,27, 33]
    },
    t2: {
        1: [10, 12, 17,44,48, 49],
        2: [13, 15, 17, 18, 32, 39],
        3: [16, 17, 20, 45, 48, 49],
        4: [6, 16, 18, 21, 32, 40]
    }
}

for(var j in TicketInfo )
{
    for(var p in TicketInfo[j] )
    {
        for(var i = 0; i < TicketInfo[j][p].length; i++ )
        {
             console.log(TicketInfo[j][p][i]);
        }
    }
}​

http://jsfiddle.net/J6rTj/

于 2012-04-19T09:00:56.067 回答
7

如果您从 google 来到这里,试图找到一种方法来进行快速打印以进行调试,那么这里有一个适合您的方法:

console.log(myArray.join("\n"))

例子:

var myArray = [[1,2,3],[4,5,6],[7,8,9]];
console.log(myArray.join("\n"));

输出:

1,2,3
4,5,6
7,8,9

带有正确括号的示例:

var myArray = [[1,2,3],[4,5,6],[7,8,9]];
console.log("[[" + myArray.join("],\n[") + "]]");

输出:

[[1,2,3],
[4,5,6],
[7,8,9]]

回答OP的问题:

obj =  {
    1: [7, 12, 35,39,41, 43],
    2: [7, 15, 20,34,45, 48],
    3: [3, 7, 10, 13, 22, 43],
    4: [2, 4, 5,23,27, 33],
}
var keys = Object.keys(obj);
keys.sort();
console.log(keys);
var listFromObj = []
for (var i = 0; i < keys.length; i++) {
    if (obj.hasOwnProperty(keys[i])) listFromObj.push(obj[keys[i]]);
}
console.log("[" + listFromObj.join("]\n[") + "]");

输出:

[7,12,35,39,41,43]
[7,15,20,34,45,48]
[3,7,10,13,22,43]
[2,4,5,23,27,33]
于 2016-02-09T18:50:19.867 回答
1

语法是TicketInfo["t1"]["1"][0].

这个例子会给你7

TicketInfo["t1"]["1"]将在您的问题的基础上为您提供您所追求的数组。

于 2012-04-19T09:01:38.673 回答
0

在您的代码t中仅代表密钥。

试试下面的代码:

var TicketInfo =
    {
        t1: {
            1: [7, 12, 35,39,41, 43],
            2: [7, 15, 20,34,45, 48],
            3: [3, 7, 10, 13, 22, 43],
            4: [2, 4, 5,23,27, 33]
        },
        t2: {
            1: [10, 12, 17,44,48, 49],
            2: [13, 15, 17, 18, 32, 39],
            3: [16, 17, 20, 45, 48, 49],
            4: [6, 16, 18, 21, 32, 40]
        }
}

for(t in TicketInfo["t1"])
{
    i++;
        console.log(TicketInfo["t1"][t]);
}
于 2012-04-19T09:02:47.453 回答
0

我知道您想按顺序输出整个表格吗?由于您在 t1/t2 级别使用对象,因此您必须为此执行额外的步骤。

首先,看看你是否可以简单地用真实数组替换对象:

var TicketInfoArrays = {
    t1: [
        [7, 12, 35,39,41, 43],
        [7, 15, 20,34,45, 48],
        [3, 7, 10, 13, 22, 43],
        [2, 4, 5,23,27, 33]
    ]
}


var t1 = TicketInfoArrays.t1

for(var idx = 0, len = t1.length; idx<len; idx++){
    var line = idx+": ["
    var nested = t1[idx]
    for(var idx2 = 0, len2 = nested.length; idx2<len2; idx2++){
        line += ((idx2 > 0 ? ', ':'') + nested[idx2])
    }
    console.log(line + ']')
}

如果这在某种程度上是不可能的,但你确定这些对象中的键总是从某个特定的数字开始并且没有间隙地递增,你可以简单地迭代属性,直到你点击空元素:

var TicketInfo = {
    t1: {
        1: [7, 12, 35,39,41, 43],
        2: [7, 15, 20,34,45, 48],
        3: [3, 7, 10, 13, 22, 43],
        4: [2, 4, 5,23,27, 33]
    }
}

var t1 =  TicketInfo.t1

var idx = 1
var nested
while(nested = t1[idx]){
    var line = idx+": ["
    var nested = t1[idx]
    for(var idx2 = 0, len2 = nested.length; idx2<len2; idx2++){
        line += ((idx2 > 0 ? ', ':'') + nested[idx2])
    }
    console.log(line + ']')
    idx++
}

最后,如果你不能保证,你将不得不手动收集所有的键,对它们进行排序,然后遍历这个排序列表。

var TicketInfoUnordered = {
    t1: {
        8: [7, 12, 35,39,41, 43],
        20: [7, 15, 20,34,45, 48],
        45: [3, 7, 10, 13, 22, 43],
        3: [2, 4, 5,23,27, 33]
    }
}

var t1 = TicketInfoUnordered.t1

var keys = []
for(key in t1){
    if(t1.hasOwnProperty(key)){ keys.push(key) }
}
keys.sort(function(a, b){ return a - b })

for(var idx = 0, len = keys.length; idx<len; idx++){
    var line = keys[idx]+": ["
    var nested = t1[keys[idx]]
    for(var idx2 = 0, len2 = nested.length; idx2<len2; idx2++){
        line += ((idx2 > 0 ? ', ':'') + nested[idx2])
    }
    console.log(line + ']')
}
于 2012-04-19T09:29:04.733 回答