输出多维数组

时间:2012-04-19 08:52:44

标签: javascript arrays multidimensional-array

我目前有以下阵列设置:

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]

这样的数组

由于

5 个答案:

答案 0 :(得分: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/

答案 1 :(得分:7)

如果你是谷歌试图找到一种快速打印进行调试的方法,那么这里有一个内容:

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]

答案 2 :(得分:1)

语法为TicketInfo["t1"]["1"][0]

该示例将为您提供7

TicketInfo["t1"]["1"]会根据您的问题为您提供您所关注的数组。

答案 3 :(得分: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]);
}

答案 4 :(得分: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 + ']')
}