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

前端 未结 17 2906
一向
一向 2020-11-22 02:59

When debugging using console.log(), how can I get the full object?

const myObject = {
   \"a\":\"a\",
   \"b\":{
      \"c\":\"c\",
      \"d\":         


        
17条回答
  •  孤街浪徒
    2020-11-22 03:19

    You can use JSON.stringify, and get some nice indentation as well as perhaps easier to remember syntax.

    console.log(JSON.stringify(myObject, null, 4));
    

    {
        "a": "a",
        "b": {
            "c": "c",
            "d": {
                "e": "e",
                "f": {
                    "g": "g",
                    "h": {
                        "i": "i"
                    }
                }
            }
        }
    }
    

    The third argument sets the indentation level, so you can adjust that as desired.

    More detail here if needed:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

提交回复
热议问题