2017-02-23 30 views
2

所以有這個NodeJS模塊叫做console.table,你可以在控制檯裏添加表格。下面是從他們的網站的例子:NodeJs console.table中的forloop

// call once somewhere in the beginning of the app 
require('console.table'); 
console.table([ 
{ 
name: 'foo', 
age: 10 
}, { 
name: 'bar', 
age: 20 
} 
]); 

// prints 
name age 
---- --- 
foo 10 
bar 20 

這僅僅是一個例子,我試圖把它在一個for循環將其自動化,我曾希望將工作的for循環和代碼是這樣的:

var values = [ 
] 

for(var l = 0;l<config.accounts.username.length;l++) { 
    values.push([ 

     { 
      username: "Number "+l, 
      itemtype: "Hello", 
      amount: 10 
     }, { 
      itemtype: "Hello", 
      amount: 10 
     } 

    ]); 
} 
console.table("", values); 

不幸的是,它不工作,有人可以幫助我嗎?

謝謝!

回答

0

你推值的數組到您的陣列 - 刪除[ & ]

for(var l = 0;l<config.accounts.username.length;l++) { 
    values.push( 
     { 
      username: "Number "+l, 
      itemtype: "Hello", 
      amount: 10 
     }, { 
      itemtype: "Hello", 
      amount: 10 
     } 

    ); 
} 

編號:Array.prototype.push

然後你原來的例子沒有采取2個參數只用了一個這樣此

console.table("", values); 

應該可能是

console.table(values); 
+0

非常感謝! –