如何以字符串格式显示JavaScript对象的内容,就像我们alert变量一样?
我希望显示对象的格式相同的方式.
在JavaScript中比较对象的最佳方法是什么?
例:
var user1 = {name : "nerd", org: "dev"};
var user2 = {name : "nerd", org: "dev"};
var eq = user1 == user2;
alert(eq); // gives false
Run Code Online (Sandbox Code Playgroud)
我知道如果它们引用完全相同的对象,则两个对象是相等的,但有没有办法检查它们是否具有相同的属性值?
以下方式对我有用,但这是唯一的可能性吗?
var eq = Object.toJSON(user1) == Object.toJSON(user2);
alert(eq); // gives true
Run Code Online (Sandbox Code Playgroud) 我有一个大对象,我想转换为JSON并发送.但它具有圆形结构.我想抛出任何存在的循环引用并发送任何可以进行字符串化的内容.我怎么做?
谢谢.
var obj = {
a: "foo",
b: obj
}
Run Code Online (Sandbox Code Playgroud)
我想将obj字符串化为:
{"a":"foo"}
Run Code Online (Sandbox Code Playgroud) 我的脚本返回[object Object]为的结果console.log(result).
可有人请解释如何有console.log归还id和name从result?
$.ajaxSetup({ traditional: true });
var uri = "";
$("#enginesOuputWaiter").show();
$.ajax({
type: "GET",
url: uri,
dataType: "jsonp",
ContentType:'application/javascript',
data :{'text' : article},
error: function(result) {
$("#enginesOuputWaiter").hide();
if(result.statusText = 'success') {
console.log("ok");
console.log(result);
} else {
$("#enginesOuput").text('Invalid query.');
}
}
});
Run Code Online (Sandbox Code Playgroud) 我有一个包含循环引用的JavaScript对象定义:它有一个引用父对象的属性.
它还具有我不想传递给服务器的功能.我如何序列化和反序列化这些对象?
我读过这样做的最好方法是使用Douglas Crockford的stringify.但是,我在Chrome中收到以下错误:
TypeError:将循环结构转换为JSON
代码:
function finger(xid, xparent){
this.id = xid;
this.xparent;
//other attributes
}
function arm(xid, xparent){
this.id = xid;
this.parent = xparent;
this.fingers = [];
//other attributes
this.moveArm = function() {
//moveArm function details - not included in this testcase
alert("moveArm Executed");
}
}
function person(xid, xparent, xname){
this.id = xid;
this.parent = xparent;
this.name = xname
this.arms = []
this.createArms = function () {
this.arms[this.arms.length] = new arm(this.id, this);
}
}
function group(xid, xparent){
this.id = …Run Code Online (Sandbox Code Playgroud) 鉴于我在一个大的JavaScript对象中有一个循环引用
我试试 JSON.stringify(problematicObject)
而浏览器抛出
"TypeError:将循环结构转换为JSON"
(预计)
那么我想找到这个循环引用的原因,最好使用Chrome开发人员工具?这可能吗?如何在大型对象中查找和修复循环引用?
我正在尝试在Chrome中对一个对象进行字符串化(...),并且我不断获得"将循环结构转换为JSON"消息,尽管事实上(据我所知)没有这样的结构存在.
我已经完成了十几次代码,无法找到任何循环引用.有没有什么方法可以让Chrome告诉我除了这个痛苦无用的错误信息之外还有什么蠢货?
我有一个tableDnD与JSON.stringify拖放:
jQuery(document).ready(function() {
jQuery("#Table").tableDnD({
onDragClass: "danger",
onDrop: function(table, row) {
jQuery.ajax({
url: "ajax.php",
type: "post",
data: {
'rows' : JSON.stringify(table.tBodies[0].rows)
},
dataType: 'html',
success: function(reponse) {
if(reponse) {
//alert('Success');
} else {
alert('Erreur');
}
}
});
}
});
});
Run Code Online (Sandbox Code Playgroud)
我有这个错误消息:
未捕获的TypeError:将循环结构转换为JSON
我只在Chrome上遇到问题.
我有一个云函数,用于交叉引用两个列表,并查找列表中彼此匹配的值.该功能似乎工作正常,但在日志中我一直看到这一点Error serializing return value: TypeError: Converting circular structure to JSON.这是功能......
exports.crossReferenceContacts = functions.database.ref('/cross-ref-contacts/{userId}').onWrite(event => {
if (event.data.previous.exists()) {
return null;
}
const userContacts = event.data.val();
const completionRef = event.data.adminRef.root.child('completed-cross-ref').child(userId);
const removalRef = event.data.ref;
var contactsVerifiedOnDatabase ={};
var matchedContacts= {};
var verifiedNumsRef = event.data.adminRef.root.child('verified-phone-numbers');
return verifiedNumsRef.once('value', function(snapshot) {
contactsVerifiedOnDatabase = snapshot.val();
for (key in userContacts) {
//checks if a value for this key exists in `contactsVerifiedOnDatabase`
//if key dioes exist then add the key:value pair to matchedContacts
};
removalRef.set(null); …Run Code Online (Sandbox Code Playgroud) javascript node.js firebase firebase-realtime-database google-cloud-functions
我是 nest.js 初学者,我正在尝试用我的代码实现 Axios,但出现此错误,我想修复它。
--> starting at object with constructor 'ClientRequest'
| property 'socket' -> object with constructor 'Socket'
--- property '_httpMessage' closes the circle +188941ms
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'ClientRequest'
| property 'socket' -> object with constructor 'Socket'
--- property '_httpMessage' closes the circle
at JSON.stringify (<anonymous>)
at stringify (D:\CUSportcomplex-register\sso-reg\node_modules\express\lib\response.js:1123:12)
at ServerResponse.json (D:\CUSportcomplex-register\sso-reg\node_modules\express\lib\response.js:260:14)
at ExpressAdapter.reply (D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\platform-express\adapters\express-adapter.js:24:57)
at RouterResponseController.apply (D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\core\router\router-response-controller.js:13:36)
at D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\core\router\router-execution-context.js:173:48
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\core\router\router-execution-context.js:47:13
at async D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\core\router\router-proxy.js:9:17
Run Code Online (Sandbox Code Playgroud)
这是我的 …
javascript ×8
json ×5
jquery ×3
node.js ×2
ajax ×1
arrays ×1
axios ×1
comparison ×1
firebase ×1
nestjs ×1
object ×1
stringify ×1
tablednd ×1
typescript ×1