73

我试图将对象复制为文本,但它只显示 [object object]。在此之前,我尝试过复制推荐它是成功的,但现在不是。那是 chrome 问题吗?

我试过什么? 只需右键单击对象并从 chrome 控制台窗口存储为全局变量,然后接下来使用 copy(temp6) 命令并尝试粘贴到 notepad++ 中。


在此处输入图像描述

4

6 回答 6

70

理想情况下,它应该使用copy您编写的命令复制对象。我只是尝试过并为我工作。
您可以尝试做的其他事情是将该对象字符串化,然后复制它。
前任。

copy(JSON.stringify(temp6))
于 2016-12-08T05:49:21.997 回答
45

如果对象已经记录

  • 右键单击控制台中的对象,然后单击存储为全局
  • 变量输出将类似于 temp1
  • 在 chrome 控制台中复制并粘贴以下代码,然后按 Enter

    (function(console){
        console.save = function(data, filename){
    
        if(!data) {
            console.error('Console.save: No data')
            return;
        }
    
        if(!filename) filename = 'console.json'
    
        if(typeof data === "object"){
            data = JSON.stringify(data, undefined, 4)
        }
    
        var blob = new Blob([data], {type: 'text/json'}),
            e    = document.createEvent('MouseEvents'),
            a    = document.createElement('a')
    
        a.download = filename
        a.href = window.URL.createObjectURL(blob)
        a.dataset.downloadurl =  ['text/json', a.download, a.href].join(':')
        e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
        a.dispatchEvent(e)
     }
    })(console)
    
    • 然后就可以使用下载功能了,

控制台.保存(temp1);

-如果显示Uncaught TypeError: Converting circular structure to JSON

然后你需要 decycle JSON object 并将下面的代码粘贴到 chrome 浏览器控制台中,然后按 enter

if (typeof JSON.decycle !== "function") {
    JSON.decycle = function decycle(object, replacer) {
        "use strict";

        var objects = new WeakMap();     // object to path mappings

        return (function derez(value, path) {


            var old_path;  
            var nu;  

            if (replacer !== undefined) {
                value = replacer(value);
            }

            if (
                typeof value === "object" && value !== null &&
                !(value instanceof Boolean) &&
                !(value instanceof Date) &&
                !(value instanceof Number) &&
                !(value instanceof RegExp) &&
                !(value instanceof String)
            ) {


                old_path = objects.get(value);
                if (old_path !== undefined) {
                    return {$ref: old_path};
                }

                objects.set(value, path);

                if (Array.isArray(value)) {
                    nu = [];
                    value.forEach(function (element, i) {
                        nu[i] = derez(element, path + "[" + i + "]");
                    });
                } else {

                    nu = {};
                    Object.keys(value).forEach(function (name) {
                        nu[name] = derez(
                            value[name],
                            path + "[" + JSON.stringify(name) + "]"
                        );
                    });
                }
                return nu;
            }
            return value;
        }(object, "$"));
    };
}


if (typeof JSON.retrocycle !== "function") {
    JSON.retrocycle = function retrocycle($) {
        "use strict";

        var px = /^\$(?:\[(?:\d+|"(?:[^\\"\u0000-\u001f]|\\([\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*")\])*$/;

        (function rez(value) {



            if (value && typeof value === "object") {
                if (Array.isArray(value)) {
                    value.forEach(function (element, i) {
                        if (typeof element === "object" && element !== null) {
                            var path = element.$ref;
                            if (typeof path === "string" && px.test(path)) {
                                value[i] = eval(path);
                            } else {
                                rez(element);
                            }
                        }
                    });
                } else {
                    Object.keys(value).forEach(function (name) {
                        var item = value[name];
                        if (typeof item === "object" && item !== null) {
                            var path = item.$ref;
                            if (typeof path === "string" && px.test(path)) {
                                value[name] = eval(path);
                            } else {
                                rez(item);
                            }
                        }
                    });
                }
            }
        }($));
        return $;
    };
}
  • 然后最后执行下载代码。

console.save(JSON.decycle(temp1));

于 2018-04-02T06:12:13.213 回答
9

您可以在控制台中使用如下命令:假设我们的对象是:

  var object = {x:"xyz"}

现在在控制台中使用以下命令 -

 copy(JSON.stringify(object))

对象现在可用于剪贴板。您现在可以使用 Ctrl + v 来使用此对象。

于 2016-12-08T05:51:05.657 回答
4

你应该检查count对象以避免循环引用,在使用之前copy(JSON.stringify(count)),请看这里

于 2017-03-22T05:19:48.890 回答
2

有很多方法可以做到这一点。一种方法是先做JSON.stringify(yourObject)然后复制输出。

于 2016-12-08T05:49:25.410 回答
0

您也可以这样做而无需编写任何代码。至少使用更高版本的chrome。

当您右键单击对象时,您将获得以下上下文: 在此处输入图像描述

但是,如果您左键单击该行以突出显示它,右键单击控制台行,您将获得此上下文菜单: 在此处输入图像描述

“另存为...”选项将创建当前控制台日志中“原样”所有内容的文本文件 (*.log)。因此,如果您想查看更多对象,只需将其扩展至您需要的范围。

折叠示例:

let tmpArr = []; tmpArr.push([]); tmpArr[0].push({ some: 'test'}); tmpArr[0].push({ some: 'next'}); console.log(tmpArr);
VM242:1 [Array(2)]0: (2) [{…}, {…}]length: 1[[Prototype]]: Array(0)
undefined
null
null

扩展示例:

let tmpArr = []; tmpArr.push([]); tmpArr[0].push({ some: 'test'}); tmpArr[0].push({ some: 'next'}); console.log(tmpArr);
VM242:1 [Array(2)]0: Array(2)0: some: "test"[[Prototype]]: Object1: some: "next"[[Prototype]]: Objectlength: 2[[Prototype]]: Array(0)length: 1[[Prototype]]: Array(0)
undefined
null
null

于 2021-12-29T21:24:29.373 回答