为了账号安全,请及时绑定邮箱和手机立即绑定

typescript将循环引用的对象数组转为字符串有问题,求解答

typescript将循环引用的对象数组转为字符串有问题,求解答

莫回无 2019-05-07 09:53:20
传入的参数如下:[{"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]
查看完整描述

2 回答

?
开满天机

TA贡献1786条经验 获得超13个赞

当要处理的数据是循环引用的话,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"
}]
                            
查看完整回答
反对 回复 2019-05-07
?
慕的地10843

TA贡献1785条经验 获得超8个赞

你把结果放出来没多大意义,你只是把你转换的方法发出来了,转换的对象却没有发出来。我猜测下,应该是你的那个判断的地方有问题
if(cache.indexOf(value)!==-1){
return;
}
这里如果存在这个缓存,你就直接return.这里return的是一个undefined.正常情况下,碰到undefined,symbol,function会自动省略,为了保持数组的索引位置信息,用null代替.查看youdontknowjs--JSONStringification
                            
查看完整回答
反对 回复 2019-05-07
  • 2 回答
  • 0 关注
  • 1462 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信