json 如何删除Javascript对象中的对象(元素)?

ztmd8pv5  于 2022-11-19  发布在  Java
关注(0)|答案(2)|浏览(194)

如何通过ID从JavaScript对象中删除一个元素?例如,我必须删除004或007:

const obj = {
  id: '001',
  children: [
    {
      id: '002',
      children: [
        {
          id: '003',
          children: [],
        },
        {
          id: '004',
          children: [
            {
              id: '005',
              children: [],
            }
          ],
        }
      ],
    },
    {
      id: '006',
      children: [
        {
          id: '007',
          children: [],
        }
      ],
    },
  ]
}

我试着喜欢这个,找到id但下一步应该是什么。预计会从对象中删除id。

const removeById = (obj = {}, id = '') => {
  console.log('obj: ', obj)

  const search = obj.children.find(o => o.id === id)
  console.log('##search: ', search)
  if(search) {
    console.log('## parent id: ', obj.id)
    ...
  }
  if (obj.children && obj.children.length > 0) {
    obj.children.forEach(el => removeById(el, id));
  }
}

removeById(obj, '007')
klr1opcd

klr1opcd1#

你可以使用findIndex来获取数组中的位置。要从数组中删除一个元素,你需要使用splice。
然后你可以用some循环遍历子节点,并检查子节点的子节点。使用some,你可以在找到id时退出,这样你就不必继续循环了。

let obj = {
  id: '001',
  children: [
    {
      id: '002',
      children: [
        {
          id: '003',
          children: [],
        },
        {
          id: '004',
          children: [
            {
              id: '005',
              children: [],
            }
          ],
        }
      ],
    },
    {
      id: '006',
      children: [
        {
          id: '007',
          children: [],
        }
      ],
    },
  ]
}

const removeById = (parentData, removeId) => {

   // This is only the parent level!
   // If you will never delete the first level parent, this is not needed
   if (parentData.id === removeId){
     Object.keys(data).forEach(key => delete parentData[key]);
     return true;
   }
   
   function recursiveFind (children) {
     // check if any of the children have the id
     const index = children.findIndex(({id}) => id === removeId);
 
     // if we have an index
     if (index != -1) {
       // remove it
       children.splice(index, 1);
       // say we found it
       return true;
     }
   
     // Loop over the chldren check their children
     return children.some(child => recursiveFind(child.children));
   }

   return recursiveFind(parentData.children);
}

removeById(obj,'004');
removeById(obj,'007');
console.log(obj)
ct2axkht

ct2axkht2#

我们可以使用递归函数来实现

let obj = {
  id: '001',
  children: [
    {
      id: '002',
      children: [
        {
          id: '003',
          children: [],
        },
        {
          id: '004',
          children: [
            {
              id: '005',
              children: [],
            }
          ],
        }
      ],
    },
    {
      id: '006',
      children: [
        {
          id: '007',
          children: [],
        }
      ],
    },
  ]
}

const removeById = (data,id) =>{
  if(data.id === id){
    delete data.id
    delete data.children
    return
   }
   data.children.forEach(d =>{
     removeById(d,id)
   })
}

removeById(obj,'006')
console.log(obj)

更新:删除后不保留空对象

let obj = {
  id: '001',
  children: [
    {
      id: '002',
      children: [
        {
          id: '003',
          children: [],
        },
        {
          id: '004',
          children: [
            {
              id: '005',
              children: [],
            }
          ],
        }
      ],
    },
    {
      id: '006',
      children: [
        {
          id: '007',
          children: [],
        }
      ],
    },
  ]
}

let match = false
const removeById = (data,id) =>{
  match = data.some(d => d.id == id)
  if(match){
      data = data.filter(d => d.id !== id)
   }else{
     data.forEach(d =>{
       d.children = removeById(d.children,id)
     })
   }
   return data
}

let data = [obj]
console.log(removeById(data,'004'))

相关问题