typescript将循环引用的对象数组转为字符串,结果有些数据变为null或者被加入null,求解!大佬们有什么好的建议?

传入的参数如下:[{
"id":"02c76c921d5011e987c600163e0e2174",
"code":"02c76c921d5011e987c600163e0e2174",
"fullTitle":"test,test-t",
"parentId":"e8bbcff61d4f11e987c600163e0e2174",
"_checked":false,
"isChecked":false,
"selected":false,
"hovered":false,
"expanded":true,
"load":true,
"parent":{
"id":"e8bbcff61d4f11e987c600163e0e2174",
"code":"e8bbcff61d4f11e987c600163e0e2174",
"fullTitle":"test",
"parentId":0,
"_checked":false,
"isChecked":false,
"children":[null,{
"id":"2473dea61d5011e987c600163e0e2174",
"code":"2473dea61d5011e987c600163e0e2174",
"fullTitle":"test,test-e",
"parentId":"e8bbcff61d4f11e987c600163e0e2174",
"_checked":false,
"isChecked":false,
"selected":false,
"hovered":false,
"expanded":true,
"load":true,
"level":2,
"isShow":true,
"spaceHtml":"",
"hasChildren":false,
"sort":"34",
"seq":2,
"isLabelShow":true,
"isInputShow":false,
"showInput":false,
"type":"editSort"
}],
"selected":false,
"hovered":false,
"expanded":true,
"load":true,
"parent":null,
"level":1,
"isShow":true,
"spaceHtml":"",
"hasChildren":true,
"sort":401,
"seq":6,
"isLabelShow":true,
"isInputShow":false,
"showInput":false
},
"level":2,
"isShow":true,
"spaceHtml":"",
"hasChildren":false,
"sort":"234",
"seq":1,
"isLabelShow":true,
"isInputShow":false,
"showInput":false,
"type":"editSort"
},{
"id":"02c76c921d5011e987c600163e0e2174",
"code":"02c76c921d5011e987c600163e0e2174",
"fullTitle":"test,test-e",
"parentId":"e8bbcff61d4f11e987c600163e0e2174",
"_checked":false,
"isChecked":false,
"selected":false,
"hovered":false,
"expanded":true,
"load":true,
"parent":{
"id":"e8bbcff61d4f11e987c600163e0e2174",
"code":"e8bbcff61d4f11e987c600163e0e2174",
"fullTitle":"test",
"parentId":0,
"_checked":false,
"isChecked":false,
"children":[null,{
"id":"2473dea61d5011e987c600163e0e2174",
"code":"2473dea61d5011e987c600163e0e2174",
"fullTitle":"test,test-e",
"parentId":"e8bbcff61d4f11e987c600163e0e2174",
"_checked":false,
"isChecked":false,
"selected":false,
"hovered":false,
"expanded":true,
"load":true,
"level":2,
"isShow":true,
"spaceHtml":"",
"hasChildren":false,
"sort":"34",
"seq":2,
"isLabelShow":true,
"isInputShow":false,
"showInput":false,
"type":"editSort"
}],
"selected":false,
"hovered":false,
"expanded":true,
"load":true,
"parent":null,
"level":1,
"isShow":true,
"spaceHtml":"",
"hasChildren":true,
"sort":401,
"seq":6,
"isLabelShow":true,
"isInputShow":false,
"showInput":false
},
"level":2,
"isShow":true,
"spaceHtml":"",
"hasChildren":false,
"sort":"234",
"seq":1,
"isLabelShow":true,
"isInputShow":false,
"showInput":false,
"type":"editSort"
},{
"id":"35f29a5e1d5011e987c600163e0e2174",
"code":"35f29a5e1d5011e987c600163e0e2174",
"fullTitle":"test-sun,test-u",
"parentId":"f24bd87b1d4f11e987c600163e0e2174",
"_checked":false,
"isChecked":false,
"selected":true,
"hovered":false,
"expanded":true,
"load":true,
"parent":{
"id":"f24bd87b1d4f11e987c600163e0e2174",
"code":"f24bd87b1d4f11e987c600163e0e2174",
"fullTitle":"test-sun",
"parentId":0,
"_checked":false,
"isChecked":false,
"children":[null,{
"id":"0ef954591d5011e987c600163e0e2174",
"code":"0ef954591d5011e987c600163e0e2174",
"fullTitle":"test-sun,test-s",
"parentId":"f24bd87b1d4f11e987c600163e0e2174",
"_checked":false,
"isChecked":false,
"selected":false,
"hovered":false,
"expanded":true,
"load":true,
"level":2,
"isShow":true,
"spaceHtml":"",
"hasChildren":false,
"sort":"456",
"seq":2,
"isLabelShow":false,
"isInputShow":true,
"showInput":false,
"type":"editSort"
}],
"selected":false,
"hovered":false,
"expanded":true,
"load":true,
"parent":null,
"level":1,
"isShow":true,
"spaceHtml":"",
"hasChildren":true,
"sort":402,
"seq":7,
"isLabelShow":true,
"isInputShow":false,
"showInput":false
},
"level":2,
"isShow":true,
"spaceHtml":"",
"hasChildren":false,
"sort":"454",
"seq":1,
"isLabelShow":true,
"isInputShow":false,
"showInput":false,
"type":"editSort"
},{
"id":"02c76c921d5011e987c600163e0e2174",
"code":"02c76c921d5011e987c600163e0e2174",
"fullTitle":"test,test-n",
"parentId":"e8bbcff61d4f11e987c600163e0e2174",
"_checked":false,
"isChecked":false,
"selected":false,
"hovered":false,
"expanded":true,
"load":true,
"parent":{
"id":"e8bbcff61d4f11e987c600163e0e2174",
"code":"e8bbcff61d4f11e987c600163e0e2174",
"fullTitle":"test",
"parentId":0,
"_checked":false,
"isChecked":false,
"children":[null,{
"id":"2473dea61d5011e987c600163e0e2174",
"code":"2473dea61d5011e987c600163e0e2174",
"fullTitle":"test,test-e",
"parentId":"e8bbcff61d4f11e987c600163e0e2174",
"_checked":false,
"isChecked":false,
"selected":false,
"hovered":false,
"expanded":true,
"load":true,
"level":2,
"isShow":true,
"spaceHtml":"",
"hasChildren":false,
"sort":"34",
"seq":2,
"isLabelShow":true,
"isInputShow":false,
"showInput":false,
"type":"editSort"
}],
"selected":false,
"hovered":false,
"expanded":true,
"load":true,
"parent":null,
"level":1,
"isShow":true,
"spaceHtml":"",
"hasChildren":true,
"sort":401,
"seq":6,
"isLabelShow":true,
"isInputShow":false,
"showInput":false
},
"level":2,
"isShow":true,
"spaceHtml":"",
"hasChildren":false,
"sort":"234",
"seq":1,
"isLabelShow":true,
"isInputShow":false,
"showInput":false,
"type":"editSort"
}]letstr=JSON.stringify(pd,function(key:any,value:any){
if(typeofvalue==='object'&&value!==null){
if(cache.indexOf(value)!==-1){
return;
}
cache.push(value);
}
returnvalue;
});用的是将上面那段代码去转换,结果应该返回的是一个数组里面有四个对象,但是其中两个却变为null,求解!谢谢![{
"id":"02c76c921d5011e987c600163e0e2174",
"code":"02c76c921d5011e987c600163e0e2174",
"fullTitle":"test,test-t",
"parentId":"e8bbcff61d4f11e987c600163e0e2174",
"_checked":false,
"isChecked":false,
"selected":false,
"hovered":false,
"expanded":true,
"load":true,
"parent":{
"id":"e8bbcff61d4f11e987c600163e0e2174",
"code":"e8bbcff61d4f11e987c600163e0e2174",
"fullTitle":"test",
"parentId":0,
"_checked":false,
"isChecked":false,
"children":[null,{
"id":"2473dea61d5011e987c600163e0e2174",
"code":"2473dea61d5011e987c600163e0e2174",
"fullTitle":"test,test-e",
"parentId":"e8bbcff61d4f11e987c600163e0e2174",
"_checked":false,
"isChecked":false,
"selected":false,
"hovered":false,
"expanded":true,
"load":true,
"level":2,
"isShow":true,
"spaceHtml":"",
"hasChildren":false,
"sort":"34",
"seq":2,
"isLabelShow":true,
"isInputShow":false,
"showInput":false,
"type":"editSort"
}],
"selected":false,
"hovered":false,
"expanded":true,
"load":true,
"parent":null,
"level":1,
"isShow":true,
"spaceHtml":"",
"hasChildren":true,
"sort":401,
"seq":6,
"isLabelShow":true,
"isInputShow":false,
"showInput":false
},
"level":2,
"isShow":true,
"spaceHtml":"",
"hasChildren":false,
"sort":"234",
"seq":1,
"isLabelShow":true,
"isInputShow":false,
"showInput":false,
"type":"editSort"
},null,{
"id":"35f29a5e1d5011e987c600163e0e2174",
"code":"35f29a5e1d5011e987c600163e0e2174",
"fullTitle":"test-sun,test-u",
"parentId":"f24bd87b1d4f11e987c600163e0e2174",
"_checked":false,
"isChecked":false,
"selected":true,
"hovered":false,
"expanded":true,
"load":true,
"parent":{
"id":"f24bd87b1d4f11e987c600163e0e2174",
"code":"f24bd87b1d4f11e987c600163e0e2174",
"fullTitle":"test-sun",
"parentId":0,
"_checked":false,
"isChecked":false,
"children":[null,{
"id":"0ef954591d5011e987c600163e0e2174",
"code":"0ef954591d5011e987c600163e0e2174",
"fullTitle":"test-sun,test-s",
"parentId":"f24bd87b1d4f11e987c600163e0e2174",
"_checked":false,
"isChecked":false,
"selected":false,
"hovered":false,
"expanded":true,
"load":true,
"level":2,
"isShow":true,
"spaceHtml":"",
"hasChildren":false,
"sort":"456",
"seq":2,
"isLabelShow":false,
"isInputShow":true,
"showInput":false,
"type":"editSort"
}],
"selected":false,
"hovered":false,
"expanded":true,
"load":true,
"parent":null,
"level":1,
"isShow":true,
"spaceHtml":"",
"hasChildren":true,
"sort":402,
"seq":7,
"isLabelShow":true,
"isInputShow":false,
"showInput":false
},
"level":2,
"isShow":true,
"spaceHtml":"",
"hasChildren":false,
"sort":"454",
"seq":1,
"isLabelShow":true,
"isInputShow":false,
"showInput":false,
"type":"editSort"
},null]
拉风的咖菲猫
浏览 580回答 2
2回答

30秒到达战场

当要处理的数据是循环引用的话,JSON.stringify是无法将数据转为字符串的,所以我目前就引用这个importcircularJsonfrom'circular-json';来处理数据会比较简单以下是处理之后的数据,看到属性parent被处理了[{"id":0,"code":0,"name":"save","status":"","remark":"","parentId":0,"_checked":false,"isChecked":false,"children":[{"id":0,"code":0,"name":"s","status":"","remark":"","parentId":0,"_checked":false,"isChecked":false,"children":[{"id":0,"code":0,"name":"ss","status":"","remark":"","parentId":0,"_checked":false,"isChecked":false,"selected":false,"hovered":false,"expanded":true,"load":true,"parent":"~0~children~0","level":3,"isShow":true,"spaceHtml":"","hasChildren":false,"seq":1,"sort":0,"isLabelShow":false,"isInputShow":true,"type":"insert"}],"selected":true,"hovered":false,"expanded":true,"load":true,"parent":"~0","level":2,"isShow":true,"spaceHtml":"","hasChildren":false,"seq":1,"sort":0,"isLabelShow":true,"isInputShow":false,"type":"insert"},{"id":0,"code":0,"name":"a","status":"","remark":"","parentId":0,"_checked":false,"isChecked":false,"selected":false,"hovered":false,"expanded":true,"load":true,"parent":"~0","level":2,"isShow":true,"spaceHtml":"","hasChildren":false,"seq":2,"sort":0,"isLabelShow":true,"isInputShow":false,"type":"insert"},{"id":0,"code":0,"name":"v","status":"","remark":"","parentId":0,"_checked":false,"isChecked":false,"selected":false,"hovered":false,"expanded":true,"load":true,"parent":"~0","level":2,"isShow":true,"spaceHtml":"","hasChildren":false,"seq":3,"sort":0,"isLabelShow":true,"isInputShow":false,"type":"insert"},{"id":0,"code":0,"name":"e","status":"","remark":"","parentId":0,"_checked":false,"isChecked":false,"selected":false,"hovered":false,"expanded":true,"load":true,"parent":"~0","level":2,"isShow":true,"spaceHtml":"","hasChildren":false,"seq":4,"sort":0,"isLabelShow":true,"isInputShow":false,"type":"insert"}],"selected":false,"hovered":false,"expanded":true,"load":true,"parent":null,"level":1,"isShow":true,"spaceHtml":"","hasChildren":false,"seq":33,"sort":0,"isLabelShow":true,"isInputShow":false,"type":"insert"}]

慕村225694

你把结果放出来没多大意义,你只是把你转换的方法发出来了,转换的对象却没有发出来。我猜测下,应该是你的那个判断的地方有问题if(cache.indexOf(value)!==-1){return;}这里如果存在这个缓存,你就直接return.这里return的是一个undefined.正常情况下,碰到undefined,symbol,function会自动省略,为了保持数组的索引位置信息,用null代替.查看youdontknowjs--JSONStringification
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript