Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度为2/4的阵列到树_Angular_Typescript - Fatal编程技术网

Angular 角度为2/4的阵列到树

Angular 角度为2/4的阵列到树,angular,typescript,Angular,Typescript,我有以下格式的数组 [{"pk_location_id":1,"locationname":"Oman","parent_location_id":0,"has_children":true}, {"pk_location_id":2,"locationname":"Muscat","parent_location_id":1,"has_children":true}, {"pk_location_id":3,"locationname":"Seeb","parent_location_id"

我有以下格式的数组

[{"pk_location_id":1,"locationname":"Oman","parent_location_id":0,"has_children":true},
 {"pk_location_id":2,"locationname":"Muscat","parent_location_id":1,"has_children":true},
{"pk_location_id":3,"locationname":"Seeb","parent_location_id":1,"has_children":false},
{"pk_location_id":4,"locationname":"Ruwi","parent_location_id":2,"has_children":false}]
我想把上面的数组转换成树,参考了下面的文档

以下是我收到的堆栈跟踪错误:-

错误类型错误:对象(…)不是函数 在SafeSubscriber.\u下一步(下拉树视图选择demo.component.ts:91) 在SafeSubscriber.webpackJsonp…/../../../../rxjs/_esm5/Subscriber.js.SafeSubscriber.\uu tryOrUnsub(Subscriber.js:239) 在SafeSubscriber.webpackJsonp…/../../../../rxjs/_esm5/Subscriber.js.SafeSubscriber.next(Subscriber.js:186) 在Subscriber.webpackJsonp…/../../../../rxjs/_esm5/Subscriber.js.Subscriber.\u下一步(Subscriber.js:127) 在Subscriber.webpackJsonp…/../../../../rxjs/_esm5/Subscriber.js.Subscriber.next(Subscriber.js:91) 在CatchSubscriber.webpackJsonp…/../../../../rxjs/_esm5/Subscriber.js.Subscriber.\u下一步(Subscriber.js:127) 在CatchSubscriber.webpackJsonp…/../../../../rxjs/_esm5/Subscriber.js.Subscriber.next(Subscriber.js:91) 在MapSubscriber.webpackJsonp…/../../../../rxjs/_esm5/operators/map.js.MapSubscriber.\u下一步(map.js:85) 在MapSubscriber.webpackJsonp…/../../../../rxjs/_esm5/Subscriber.js.Subscriber.next(Subscriber.js:91)

在这件事上有谁能帮我吗

谢谢
Amruta Wandakar

我认为您的数组应该使用
id
pid
而不是
pk\u-location\u-id
parent\u-location\u-id

在链接到的页面上运行它时,以下操作有效。见():


这在链接中效果很好,但我在angular 2中使用了它,因此我面临错误(在我的帖子中已经给出了堆栈跟踪),请在这方面帮助我。谢天谢地,如果看不到角度代码,我帮不了什么忙。看起来您使用了/ngx treeview中的
下拉列表treeview select demo.component.ts
,并正在对其进行修改。错误似乎出现在组件的第91行。看起来那里的可观察对象的
.subscribe
处理程序中有错误。是的,我正在使用ngx treeview,这需要父级和子级格式的数据,但我将获得平面数组,因此我使用了arraytotree方法,但不起作用。是否有任何方法将数组转换为树以提供ngx treeview。
array2tree(
[{"id":1,"locationname":"Oman","pid":0,"has_children":true}, 
 {"id":2,"locationname":"Muscat","pid":1,"has_children":true}, 
 {"id":3,"locationname":"Seeb","pid":1,"has_children":false}, 
 {"id":4,"locationname":"Ruwi","pid":2,"has_children":false}
])