mrj*_*mrj 13 javascript console.log
我在控制台中查看数据数组.
console.table(myArray)始终将索引作为第一列.这在查看对象数据时很好,当索引是键时,但是当索引是数组索引时(在我的情况下它是分散注意力/烦人)有没有办法显示没有这个索引的表?可选的columns参数允许只显示想要的列...索引除外.
小智 15
如果您使用的是节点(而不是浏览器)并且您想要一个不引入依赖项的解决方案,您可以这样做:
\n(建立在这个答案的基础上):
\nconst { Console } = require(\'console\');\nconst { Transform } = require(\'stream\');\n\nfunction table(input) {\n // @see /sf/answers/4750156911/\n const ts = new Transform({ transform(chunk, enc, cb) { cb(null, chunk) } })\n const logger = new Console({ stdout: ts })\n logger.table(input)\n const table = (ts.read() || \'\').toString()\n let result = \'\';\n for (let row of table.split(/[\\r\\n]+/)) {\n let r = row.replace(/[^\xe2\x94\xac]*\xe2\x94\xac/, \'\xe2\x94\x8c\');\n r = r.replace(/^\xe2\x94\x9c\xe2\x94\x80*\xe2\x94\xbc/, \'\xe2\x94\x9c\');\n r = r.replace(/\xe2\x94\x82[^\xe2\x94\x82]*/, \'\');\n r = r.replace(/^\xe2\x94\x94\xe2\x94\x80*\xe2\x94\xb4/, \'\xe2\x94\x94\');\n r = r.replace(/\'/g, \' \');\n result += `${r}\\n`;\n }\n console.log(result);\n}\n\nconst test = [\n { name: "Jane", id: \'1234\', pastime: \'Archery\' },\n { name: "John", id: \'1235\', pastime: \'Knitting\' },\n { name: "Jess", id: \'1236\', pastime: \'Fishing\' }\n];\n\ntable(test);\nRun Code Online (Sandbox Code Playgroud)\n此表中没有索引列的结果:
\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 name \xe2\x94\x82 id \xe2\x94\x82 pastime \xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n\xe2\x94\x82 Jane \xe2\x94\x82 1234 \xe2\x94\x82 Archery \xe2\x94\x82\n\xe2\x94\x82 John \xe2\x94\x82 1235 \xe2\x94\x82 Knitting \xe2\x94\x82\n\xe2\x94\x82 Jess \xe2\x94\x82 1236 \xe2\x94\x82 Fishing \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\nRun Code Online (Sandbox Code Playgroud)\n
表中的第一列将被标记为(索引)。如果data是一个数组,则其值将是数组索引。如果data是一个对象,则其值将是属性名称。请注意(在Firefox中)console.table只能显示1000行(第一行是标签索引)。
因此,对于数组,您无法隐藏要显示的索引键。但是,作为一个解决方法,你可以在阵列转换到您使用对象的钥匙。
例:
const array = [{myId: 42, name: 'John', color: 'red'}, {myId: 1337, name: 'Jane', color: 'blue'}]
const transformed = array.reduce((acc, {myId, ...x}) => { acc[myId] = x; return acc}, {})
console.table(transformed)Run Code Online (Sandbox Code Playgroud)