Javascript / Chrome - How to copy an object from the webkit inspector as code

后端 未结 11 1903
天涯浪人
天涯浪人 2020-11-29 14:47

I am doing a console.log statement in my javascript in order to log a javascript object. I\'m wondering if there\'s a way, once that\'s done - to copy that object as javascr

相关标签:
11条回答
  • 2020-11-29 15:08

    This should help stringify deep objects by leaving out recursive Window and Node objects.

    function stringifyObject(e) {
      const obj = {};
      for (let k in e) {
        obj[k] = e[k];
      }
    
      return JSON.stringify(obj, (k, v) => {
        if (v instanceof Node) return 'Node';
        if (v instanceof Window) return 'Window';
        return v;
      }, ' ');
    }
    
    0 讨论(0)
  • 2020-11-29 15:10

    Using "Store as a Global Variable" works, but it only gets the final instance of the object, and not the moment the object is being logged (since you're likely wanting to compare changes to the object as they happen). To get the object at its exact point in time of being modified, I use this...

    function logObject(object) {
        console.info(JSON.stringify(object).replace(/,/g, ",\n"));
    }
    

    Call it like so...

    logObject(puzzle);
    

    You may want to remove the .replace(/./g, ",\n") regex if your data happens to have comma's in it.

    0 讨论(0)
  • 2020-11-29 15:11

    Follow the following steps:

    1. Output the object with console.log from your code, like so: console.log(myObject)
    2. Right click on the object and click "Store as Global Object". Chrome would print the name of the variable at this point. Let's assume it's called "temp1".
    3. In the console, type: JSON.stringify(temp1).
    4. At this point you will see the entire JSON object as a string that you can copy/paste.
    5. You can use online tools like http://www.jsoneditoronline.org/ to prettify your string at this point.
    0 讨论(0)
  • 2020-11-29 15:11

    Right click on data which you want to store

    • Firstly, Right click on data which you want to store -> select "Store as global variable" And the new temp variable appear like bellow: (temp3 variable): New temp variable appear in console
    • Second use command copy(temp_variable_name) like picture: enter image description here After that, you can paste data to anywhere you want. hope useful/
    0 讨论(0)
  • 2020-11-29 15:12

    You can now accomplish this in Chrome by right clicking on the object and selecting "Store as Global Variable": http://www.youtube.com/watch?v=qALFiTlVWdg

    0 讨论(0)
提交回复
热议问题