Sandeep Bansal
Sandeep Bansal

Reputation: 6394

Output multi-dimensional arrays

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

Upvotes: 0

Views: 12890

Answers (5)

Bryant Kou
Bryant Kou

Reputation: 1739

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]

Upvotes: 7

Oleg V. Volkov
Oleg V. Volkov

Reputation: 22421

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

Upvotes: 0

Epuri
Epuri

Reputation: 300

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

Upvotes: 0

Marty
Marty

Reputation: 39456

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.

Upvotes: 1

Boyo
Boyo

Reputation: 1381

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/

Upvotes: 8

Related Questions