Search code examples
google-chrome-devtools

Is there a way to auto expand objects in Chrome Dev Tools?


EVERY SINGLE TIME I view an object in the console I am going to want to expand it, so it gets tiresome to have to click the arrow to do this EVERY SINGLE TIME :) Is there a shortcut or setting to have this done automatically?


Solution

  • While the solution mentioning JSON.stringify is pretty great for most of the cases, it has a few limitations

    • It can not handle items with circular references where as console.log can take care of such objects elegantly.
    • Also, if you have a large tree, then ability to interactively fold away some nodes can make exploration easier.

    Here is a solution that solves both of the above by creatively (ab)using console.group:

    function expandedLog(item, maxDepth = 100, depth = 0){
        if (depth > maxDepth ) {
            console.log(item);
            return;
        }
        if (typeof item === 'object' && item !== null) {
            Object.entries(item).forEach(([key, value]) => {
                console.group(key + ' : ' +(typeof value));
                expandedLog(value, maxDepth, depth + 1);
                console.groupEnd();
            });
        } else {
            console.log(item);
        }
    }
    

    Now running:

    expandedLog({
        "glossary": {
            "title": "example glossary",
            "GlossDiv": {
                "title": "S",
                "GlossList": {
                    "GlossEntry": {
                        "ID": "SGML",
                        "SortAs": "SGML",
                        "GlossTerm": "Standard Generalized Markup Language",
                        "Acronym": "SGML",
                        "Abbrev": "ISO 8879:1986",
                        "GlossDef": {
                            "para": "A meta-markup language, used to create markup languages such as DocBook.",
                            "GlossSeeAlso": ["GML", "XML"]
                        },
                        "GlossSee": "markup"
                    }
                }
            }
        }
    })
    

    Will give you something like:

    output screenshot

    The value of maxDepth can be adjusted to a desired level, and beyond that level of nesting - expanded log will fall back to usual console.log

    Try running something like:

    x = { a: 10, b: 20 }
    x.x = x 
    expandedLog(x)
    

    enter image description here

    Also please note that console.group is non-standard.