【问题标题】:the content is too long, How to display the complete content in console.log()?内容太长,如何在console.log()中显示完整的内容?
【发布时间】:2022-01-17 01:23:08
【问题描述】:

我有一个大数组,它有很多元素,
我需要在控制台中显示它,
我使用console.log(),但只显示了一部分。

如何显示完整的内容?

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    这里说How can I get the full object in Node.js's console.log(), rather than '[Object]'?

    您可以使用 json 直接转换您的元素(对象或数组)

    console.log('my elem : %j', myObject);
    

    或者如果你想要更多的显示选项,你可以使用 util.inspect

    const util = require('util')
    console.log(util.inspect(myObject, {showHidden: false, depth: null, colors: true}))
    

    【讨论】:

      【解决方案2】:

      你可以使用

       console.dir(myArry, {'maxArrayLength': null});
      

      控制台方法 log() 显示传递给它的任何对象的 toString 表示。控制台方法 dir() 显示指定 JavaScript 对象的属性的交互式列表

      参考链接:Dumping whole array: console.log and console.dir output "... NUM more items]"

      【讨论】:

        【解决方案3】:

        通过JSON.stringify 运行它,因此您记录的是单个字符串而不是复杂的数据结构。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-09-08
          • 2016-02-17
          • 2013-03-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多