重建一个深层嵌套的对象数组,删除不需要的对象

如何解决重建一个深层嵌套的对象数组,删除不需要的对象

我不知道如何在通过 id 值指向它的深嵌套对象数组中删除和对象。

这是我使用 reduce 的代码。我想在每次迭代时重新创建数组,因为它很混乱(而且它只通过索引 0)。

我想通过指向节点 ID 35993 来删除整个部分。 如果节点获得一个子节点,这也会删除子节点。

const array = [
  {
    node: {
      level: 0,id: 71,type: "block",style: {
        backgroundColor: "#548444",},children: [
      {
        node: {
          id: 85,style: {
            backgroundColor: "#548444",fontSize: "19px",children: [
          {
            node: {
              id: 955,type: "column",style: {
                backgroundColor: "#eee",fontSize: "28px",children: [
              {
                node: {
                  level: 3,parentId: 71,id: 732,type: "text",data: {
                    text: "Ceci est un text",style: {
                    color: "blue",fontSize: "13px",{
                node: {
                  id: 353,],{
    node: {
      level: 0,id: 7991,children: [
      {
        node: {
          id: 8995,children: [
          {
            node: {
              id: 95995,id: 73992,{
                node: {
                  id: 35993,];

const search1 = (arr,itemId,nestingKey) =>
  arr.reduce((a,item) => {
    if (a) return a;
    if (item.node.id === itemId) return item;
    if (item[nestingKey]) return search(item[nestingKey],nestingKey);
  },null);

const remove = function (arr,nestingKey) {
  let newArr = [];
  arr.reduce((a,item) => {
    if (item.node.id !== itemId) {
      const tempItem = {
        ...item,};
      newArr.push(tempItem);
    }
      
    if (item.node.id === itemId) {
      delete item.node;
      return item;
    }
      
    if (item[nestingKey]) return remove(item[nestingKey],null);

  return newArr;
};

const res = remove(array,35993,"children");
console.log(JSON.stringify(res));

解决方法

在这里修改数组非常容易,无论如何您都不必担心删除子项,因为您删除了项目的一部分。只需找到要移除的数组项,然后拼接出来即可。

即使你不想要一个变异的数组,克隆/复制并仍然这样做可能会更容易。

示例如下。 我已经使用 id 95995 显示它也在删除孩子..

const array = [{"node":{"level":0,"id":71,"type":"block","style":{"backgroundColor":"#548444"}},"children":[{"node":{"id":85,"style":{"backgroundColor":"#548444","fontSize":"19px"}},"children":[{"node":{"id":955,"type":"column","style":{"backgroundColor":"#eee","fontSize":"28px"}},"children":[{"node":{"level":3,"parentId":71,"id":732,"type":"text","data":{"text":"Ceci est un text"},"style":{"color":"blue","fontSize":"13px"}}},{"node":{"id":353,"fontSize":"13px"}}}]}]}]},{"node":{"level":0,"id":7991,"children":[{"node":{"id":8995,"children":[{"node":{"id":95995,"id":73992,{"node":{"id":35993,"fontSize":"13px"}}}]}]}]}];

function removeById(array,id) {
  for (let ix = 0; ix < array.length; ix += 1) {
    const r = array[ix];
    if (r.node.id === id) {
      array.splice(ix,1);
    } else {
      if (r.children) removeById(r.children,id);
    }
  }
}

console.log(JSON.stringify(array));

removeById(array,95995);

console.log(JSON.stringify(array));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?