javascript 输出多维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10224691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Output multi-dimensional arrays
提问by Sandeep Bansal
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]
我希望能够像这样输出数组 [7, 12, 35,39,41, 43]
Thanks
谢谢
回答by Boyo
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]);
}
}
}?
回答by Bryant Kou
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:
如果您从 google 来到这里,试图找到一种快速打印以进行调试的方法,那么这里有一个适合您的衬里:
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:
回答 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[") + "]");
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]
回答by Marty
The syntax is TicketInfo["t1"]["1"][0]
.
语法是TicketInfo["t1"]["1"][0]
.
That example will give you 7
.
那个例子会给你7
。
TicketInfo["t1"]["1"]
will give you the array you're after at the base of your question.
TicketInfo["t1"]["1"]
将在您的问题的基础上为您提供您所追求的数组。
回答by Epuri
In your code t
just represents the key.
在您的代码中t
仅代表密钥。
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]);
}
回答by Oleg V. Volkov
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.
我明白你想按顺序输出整个表吗?由于您在 t1/t2 级别使用对象,因此您必须为此执行额外的步骤。
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 + ']')
}