javascript - 遍历嵌套数组,通过id找到具体的叶子节点,并移除

标签 javascript arrays recursion

JSFiddle

我有一个最多 6 层的嵌套数组。第 6 级称为 gls,它保存数据库中的 gl 对象。我需要做的是找到一个特定的 gl 并将其从整个数组中删除。我能够使用以下函数找到特定元素;

const removeFromData = function(nodes, id) {
  return nodes.some((node) => {
    if (node.gls) {
      node.gls.forEach((gl) => {
        if (gl.id === id) {
          console.log(gl, id);
        }
      });
    } else if (node.children) {
      return removeFromData(node.children, id);
    }
  });
}

但是,我正在努力从 data 数组中实际删除它。尝试通过 data.indexOf(gl) 获取索引显然会返回 -1,因为它不会搜索嵌套元素。实现此目标的最佳方法是什么?

const id = 1000;

const data = [{
    "id": 1, "name": "Node 1", "children": [{
      "id": 2, "name": "Node 1.1", "children": [{
        "id": 4, "name": "Node 1.1.1", "leaf": true, "children": [], "gls": [{
          "id": 1000, "name": "GL1", "code": "0100"
        }, {
          "id": 1001, "name": "GL2", "code": "0200"
        }]
      }, {
        "id": 5, "name": "Node 1.1.2", "leaf": true, "children": [], "gls": [{
          "id": 2000, "name": "GL3", "code": "0300"
        }, {
          "id": 2001, "name": "GL4", "code": "0400"
        }]
      }]
    }, {
      "id": 3, "name": "Node 1.2", "children": [{
        "id": 6, "name": "Node 1.2.1", "leaf": true, "children": [], "gls": [{
          "id": 3000, "name": "GL5", "code": "0500"
        }, {
          "id": 3001, "name": "GL6", "code": "0600"
        }]
      }]
    }]
  },
  {
    "id": 7, "name": "Node 2", "children": [{
      "id": 8, "name": "Node 2.1", "children": [{
        "id": 9, "name": "Node 2.1.1", "leaf": true, "children": [], "gls": [{
          "id": 4000, "name": "GL7", "code": "0700"
        }, {
          "id": 4001, "name": "GL8", "code": "0800"
        }]
      }]
    }]
  }
];


let removeFromData = function(nodes, id) {
  return nodes.some((node) => {
    if (node.gls) {
      node.gls.forEach((gl) => {
        if (gl.id === id) {
          document.querySelector('#target').innerText = `found ${gl.name}, ${gl.id} with needle ${id}`;
        }
      });
    } else if (node.children) {
      return removeFromData(node.children, id);
    }
  });
}

removeFromData(data, id);
<p id="target"></p>

最佳答案

forEach 将三个参数传递给回调,第二个参数是被访问条目的索引。因此,您可以将该索引与 splice 一起使用(如果您想就地修改):

const removeFromData = function(nodes, id) {
  return nodes.some((node) => {
    if (node.gls) {
      node.gls.forEach((gl, index) => {
// -----------------------^^^^^^^
        if (gl.id === id) {
          //console.log(gl, id);
          node.gls.splice(index, 1); // <=== Removes the entry
        }
      });
    } else if (node.children) {
      return removeFromData(node.children, id);
    }
  });
}

我注意到您正在使用 some,这表明您希望在找到该条目时停止并且可能还返回一个指示成功/失败的标志。如果是这样,我会在 nodes.gls 搜索中使用 some 而不是 forEach 或者可能使用 findIndex .使用一些:

const removeFromData = function(nodes, id) {
  return nodes.some((node) => {
    if (node.gls) {
      return node.gls.some((gl, index) => {
        if (gl.id === id) {
          //console.log(gl, id);
          node.gls.splice(index, 1); // <=== Removes the entry
          return true;
        }
      });
    } else if (node.children) {
      return removeFromData(node.children, id);
    }
  });
}

使用findIndex:

const removeFromData = function(nodes, id) {
  return nodes.some((node) => {
    if (node.gls) {
      const index = node.gls.findIndex((gl) => {
        return gl.id === id;
      });
      if (index === -1) {
        return false;
      }
      node.gls.splice(index, 1);
      return true;
    } else if (node.children) {
      return removeFromData(node.children, id);
    }
  });
}

关于javascript - 遍历嵌套数组,通过id找到具体的叶子节点,并移除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57072239/

相关文章:

php - Jquery/Javascript 从 PHP 范围问题获取信息?

java - 算法:合并重叠片段

java - 继承和递归

python - 在渐近分析的情况下,迭代和递归二进制搜索算法有什么区别

c++ - 为什么有限递归行为会导致崩溃? (free():无效的指针)

javascript - 未捕获的类型错误 : Cannot read property 'urlshortener' of undefined

javascript - ChartJS,条形图标签中的多行

javascript - 最初的 promise 成功后又拒绝内心的 promise ?

iphone - 2 字节数组/内联 c 数组的相等性

c - 在 C 中将 P2 .pgm 图像读入二维数组