[英]json data question parents and children create
for(var d in response.data) {
var item = response.data[d];
console.log("test-1234=="+JSON.stringify(item));
}
If you take the data, it comes out as below. 如果您获取数据,则如下所示。
test-1234=={"id":3,"isLeaf":"false","name":"bang","pid":0,"didabled":null}
test-1234=={"id":4,"isLeaf":"true","name":"test1","pid":3,"didabled":null}
test-1234=={"id":5,"isLeaf":"true","name":"test2","pid":3,"didabled":null}
test-1234=={"id":6,"isLeaf":"false","name":"test3","pid":0,"didabled":null}
I want to create a relationship between parents and children based on pid. 我想基于pid创建父母与孩子之间的关系。
Like below. 像下面。 Please give me a solution.
请给我一个解决方案。
[
{
name: 'bang',
id: 3,
pid: 0,
dragDisabled: true,
children: [
{
name: 'test1',
id: 4,
isLeaf: true,
pid: 3
},
{
name: 'test2',
id: 5,
isLeaf: true,
pid: 3
}
]
},
{
name: 'test3',
id: 6,
isLeaf: true,
pid: 0
}
]
Try like this. 尝试这样。
var arr = [ { "id": 3, "isLeaf": "false", "name": "bang", "pid": 0, "didabled": null }, { "id": 4, "isLeaf": "true", "name": "test1", "pid": 3, "didabled": null }, { "id": 5, "isLeaf": "true", "name": "test2", "pid": 3, "didabled": null }, { "id": 6, "isLeaf": "false", "name": "test3", "pid": 0, "didabled": null }, { "id": 7, "isLeaf": "true", "name": "test\\43", "pid": 4, "didabled": null } ] var res = []; for(var d in arr) { var item = arr[d]; if(item.pid != 0){ findParentAndPush(item) } else { item.children = []; res.push(item); } } function findParentAndPush(item){ var filtered = arr.filter(data => data.id === item.pid); item.children = []; filtered[0].children.push(item) } console.log(res)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.