我试图将对象复制为文本,但它只显示 [object object]。在此之前,我尝试过复制推荐它是成功的,但现在不是。那是 chrome 问题吗?
我试过什么?
只需右键单击对象并从 chrome 控制台窗口存储为全局变量,然后接下来使用 copy(temp6) 命令并尝试粘贴到 notepad++ 中。
6 回答
理想情况下,它应该使用copy
您编写的命令复制对象。我只是尝试过并为我工作。
您可以尝试做的其他事情是将该对象字符串化,然后复制它。
前任。
copy(JSON.stringify(temp6))
如果对象已经记录
- 右键单击控制台中的对象,然后单击存储为全局
- 变量输出将类似于 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));
您可以在控制台中使用如下命令:假设我们的对象是:
var object = {x:"xyz"}
现在在控制台中使用以下命令 -
copy(JSON.stringify(object))
对象现在可用于剪贴板。您现在可以使用 Ctrl + v 来使用此对象。
你应该检查count
对象以避免循环引用,在使用之前copy(JSON.stringify(count))
,请看这里
有很多方法可以做到这一点。一种方法是先做JSON.stringify(yourObject)
然后复制输出。
您也可以这样做而无需编写任何代码。至少使用更高版本的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