简体   繁体   English

从数组中查找父项和子项

[英]Find parent and child from array

In the array below, some of the items are parent and some of them are its children.在下面的数组中,有些项是父项,有些项是其子项。

I want to find parents(with depth 0) and their childrens(nested with depth 1 -- depth2 --- depth 3) and then push to another array.我想找到父母(深度为 0)和他们的孩子(嵌套深度为 1——深度 2——深度 3),然后推送到另一个数组。

So far what I did only works for depth 0 and depth 1, don't know what to do with more depths.到目前为止,我所做的仅适用于深度 0 和深度 1,不知道如何处理更多深度。

Here's the fiddle : http://jsfiddle.net/s3x5f4ap/2/这是小提琴: http : //jsfiddle.net/s3x5f4ap/2/

 const comments = [ { "depth": 0,"id": "f35vz2f"}, { "depth": 0,"id": "f359354"}, { "depth": 1,"id": "f35e0b0", "parent_id": "f359354" }, { "depth": 2, "id": "f35ji24", "parent_id": "f35e0b0"}, { "depth": 2, "id": "f35rnwb", "parent_id": ""}, { "depth": 2, "id": "f35ojh4", "parent_id": "f35e0b0" }, { "depth": 3, "id": "f35lmch", "parent_id": "f35ji24"}, { "depth": 3, "id": "f35kl96", "parent_id": "f35ji24"}] const parent = comments.filter(cm => cm.depth == 0); final = []; final = parent; comments.forEach(a => { final.forEach(c => { if (c.id == a.parent_id) { c.child = [] c.child.push(a); } }) }) console.log(final)

You could collect all relations of the nodes and build a tree.您可以收集节点的所有关系并构建一棵树。

 var data = [{ depth: 0, id: "f35vz2f" }, { depth: 0, id: "f359354" }, { depth: 1, id: "f35e0b0", parent_id: "f359354" }, { depth: 2, id: "f35ji24", parent_id: "f35e0b0" }, { depth: 2, id: "f35rnwb", parent_id: "" }, { depth: 2, id: "f35ojh4", parent_id: "f35e0b0" }, { depth: 3, id: "f35lmch", parent_id: "f35ji24" }, { depth: 3, id: "f35kl96", parent_id: "f35ji24" }], tree = function (data, root) { var t = {}; data.forEach(o => { Object.assign(t[o.id] = t[o.id] || {}, o); t[o.parent_id] = t[o.parent_id] || {}; t[o.parent_id].children = t[o.parent_id].children || []; t[o.parent_id].children.push(t[o.id]); }); return t[root].children; }(data, undefined); console.log(tree);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

If you want to end up with a tree-like structure, I would do something like:如果你想以树状结构结束,我会做这样的事情:

 var data = [{ depth: 0, id: "f35vz2f" }, { depth: 0, id: "f359354" }, { depth: 1, id: "f35e0b0", parent_id: "f359354" }, { depth: 2, id: "f35ji24", parent_id: "f35e0b0" }, { depth: 2, id: "f35rnwb", parent_id: "" }, { depth: 2, id: "f35ojh4", parent_id: "f35e0b0" }, { depth: 3, id: "f35lmch", parent_id: "f35ji24" }, { depth: 3, id: "f35kl96", parent_id: "f35ji24" }]; var attach_children_to_item = function (item, data) { item.children = data.filter(x => x.parent_id == item.id) .map(x => attach_children_to_item(x, data)); return item; }; var tree = data.filter(x => x.depth == 0) .map(x => attach_children_to_item(x, data)); console.log(tree);

Note that you will have missing items if the depth > 0 and the parent_id does not correspond to another item.请注意,如果depth > 0 且parent_id与另一个项目不对应,您将丢失项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM