繁体   English   中英

在对象数组中匹配2个属性的值,并根据第3个属性的值过滤匹配项

[英]match 2 properties' values in array of objects and filter matches based on a 3rd property's value

我有一个图,其中的节点可以连接到多个其他节点。

每个节点代表数组中的一个对象。 每个节点的对象内都有一个数组,其中包含链接到该节点的所有节点的ID及其深度:

 nodes: [
    {"id":1, "depth":0, "next":[], "children":[2, 3]},     // nodes.next = [2, 3]
    {"id":2, "depth":1, "next":[], "children":[1, 4, 5]},  // nodes.next = [4, 5]
    {"id":3, "depth":1, "next":[], "children":[1, 6, 7]},  // nodes.next = [6, 7]
    {"id":4, "depth":2, "next":[], "children":[2, 8]},     // nodes.next = [8]
    {"id":5, "depth":2, "next":[], "children":[2, 9]}      // nodes.next = [9]
] 

我想从某个节点遍历图。

问题在于节点的子节点数组包含链接到该节点的所有节点。 深度为2的节点指向深度为1的节点。

因此,我想在节点的对象内创建一个新的数组,例如nodes.next并删除指向深度小于其自身深度的节点的子级。

真正nodes.children我的部分是检查nodes.children节点的深度。 我甚至还没有接近的部分,我会看一下一个节点的深度nodes.children高于nodes[i].depthnodes[i].children[i]nodes[i].next

如果有解决此问题的更好方法,我将很高兴知道。 我的尝试在许多方面都是徒劳的:

let childDepth;
for (let i = 0; i < nodes.length; i++) {
    for (let child in nodes[i].children) {
        if (nodes.id === child) {
            childDepth = nodes[i].depth;
        }
        if (childDepth > graph.nodes[i].depth) {
            nodes[i].next.push(child)
        }
    }
}

更新的数组:

const nodes = [
    { "id": 37, "depth": 0, "children": [210, 395, 265], "next": [] },
    { "id": 210, "depth": 1, "children": [37, 260, 259, 391],"next": [] },
    { "id": 256, "depth": 2, "children": [265], "next": [] },
    { "id": 259, "depth": 2, "children": [210, 397, 396], "next": [] },
    { "id": 260, "depth": 2, "children": [210], "next": [] },
    { "id": 265, "depth": 1, "children": [37, 256, 388, 394, 271, 269], "next": [] },
    { "id": 269, "depth": 2, "children": [265], "next": [] },
    { "id": 271, "depth": 2, "children": [265], "next": [] },
    { "id": 388, "depth": 2, "children": [265], "next": [] },
    { "id": 391, "depth": 2, "children": [210], "next": [] },
    { "id": 394, "depth": 2, "children": [265], "next": [] },
    { "id": 395, "depth": 1, "children": [37], "next": [] },
    { "id": 396, "depth": 3, "children": [259, 413], "next": [] },
    { "id": 397, "depth": 3, "children": [259], "next": [] },
    { "id": 413, "depth": 4, "children": [396], "next": [] }
];

在此处输入图片说明

请看下面的代码,看它是否在寻找

const array = [
  {id:1, depth:0, next:[], children:[2, 3]},
  {id:2, depth:1, next:[], children:[1, 4, 5]},  
  {id:3, depth:1, next:[], children:[1, 6, 7]},  
  {id:4, depth:2, next:[], children:[2, 8]},    
  {id:5, depth:2, next:[], children:[2, 9]}
]

array.forEach(x => {
  let { children, depth } = x;

  for(let i=depth; i< children.length; i++){
    x.next.push(children[i]);
  }
});

输出如下:

[
  {"id":1,"depth":0,"next":[2,3],"children":[2,3]},
  {"id":2,"depth":1,"next":[4,5],"children":[1,4,5]}, 
  {"id":3,"depth":1,"next":[6,7],"children":[1,6,7]}, 
  {"id":4,"depth":2,"next":[],"children":[2,8]},
  {"id":5,"depth":2,"next":[],"children":[2,9]}
]

暂无
暂无

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

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