2017-02-23 69 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

非常感谢! –