最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Output multi-dimensional arrays - Stack Overflow

programmeradmin5浏览0评论

I currently have the following array set up:

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]
        }
        }

What I want to do is iterate through these to bring back the arrays under.

As a test I've tried something like this:

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

But it's obviously not working how I want it to.

Any ideas?

I want to be able to output the arrays like [7, 12, 35,39,41, 43]

Thanks

I currently have the following array set up:

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]
        }
        }

What I want to do is iterate through these to bring back the arrays under.

As a test I've tried something like this:

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

But it's obviously not working how I want it to.

Any ideas?

I want to be able to output the arrays like [7, 12, 35,39,41, 43]

Thanks

Share Improve this question asked Apr 19, 2012 at 8:52 Sandeep BansalSandeep Bansal 6,39418 gold badges88 silver badges128 bronze badges 2
  • In your Write, change from Write(t.i) to Write(t[i]). t[i] will give you the corresponding array as you asked – Mohan Kumar Commented Apr 19, 2012 at 9:10
  • The OP's example isn't actually a multidimensional array. this answer, however, does provide a general solution if you actually have an array of arrays. – c32hedge Commented Apr 16, 2018 at 21:22
Add a ment  | 

5 Answers 5

Reset to default 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/J6rTj/

If you're here from google trying to find a way to do a quick print for debugging, here's a one liner for you:

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

Example:

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

Output:

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

Example with proper brackets:

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

Output:

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

Answer to OP's question:

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[") + "]");

Output:

[7,12,35,39,41,43]
[7,15,20,34,45,48]
[3,7,10,13,22,43]
[2,4,5,23,27,33]

The syntax is TicketInfo["t1"]["1"][0].

That example will give you 7.

TicketInfo["t1"]["1"] will give you the array you're after at the base of your question.

In your code t just represents the key.

Try following code:

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

Do I understand that you want to output entire table in order? Since you use objects on t1/t2 level, you'll have to do extra steps for that.

First, see if you can simply replace objects with real arrays:

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 + ']')
}

If that's somehow impossible, but you sure that keys in those objects always start at some specific number and go ascending without gaps, you can simply itreate over properties until you hit empty element:

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++
}

Finally, if you can't guarantee even that, you will have to manually collect all keys, sort them and then iterate over this sorted list.

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 + ']')
}
发布评论

评论列表(0)

  1. 暂无评论