javascript 如何从嵌套对象中删除基于属性的对象(元素)

wlp8pajw  于 2022-11-20  发布在  Java
关注(0)|答案(2)|浏览(136)

如何从嵌套的对象中删除基于属性的对象(元素)。例如,我需要删除类型为的所有对象:从obj树中删除'绿色'如果obj有子级,则也将删除它。

const obj = {
  id: '01',
  children: [
    {
      id: '02',
      type: 'green',
      children: [
        {
          id: '03',
          type: 'black',
          children: [],
        },
        {
          id: '04',
          type: 'green',
          children: [
            {
              id: '05',
              type: 'white',
              children: [],
            }
          ],
        }
      ],
    },
    {
      id: '06',
      type: 'black',
      children: [
        {
          id: '07',
          type: 'green',
          children: [],
        },
        {
          id: '08',
          type: 'white',
          children: [
            {
              id: '09',
              type: 'green',
              children: [],
            }
          ],
        }
      ],
    },
  ]
}

//预期结果(如果移除类型:“绿色”)

const expectedObj = {
  id: '01',
  type: 'orange',
  children: [
    {
      id: '06',
      type: 'black',
      children: [
        {
          id: '08',
          type: 'white',
          children: [],
        }
      ],
    },
  ]
}

我想做的是

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

let data = obj.children
console.dir(removeByType(data, 'black'), { depth: null })

但是{ id:'03',类型:“黑人”,孩子:[] }仍在对象树中

ht4b089n

ht4b089n1#

您已经很接近了,只是没有注意到每个示例都是一个数组,您必须通过map

const removeByType = (data, type) => {
  data = data.filter(d => d.type !== type)
  data = data.map(d => {
    d.children = removeByType(d.children, type);
    return d;
  })
  return data
}
const obj = {
  id: '01',
  children: [{
      id: '02',
      type: 'green',
      children: [{
          id: '03',
          type: 'black',
          children: [],
        },
        {
          id: '04',
          type: 'green',
          children: [{
            id: '05',
            type: 'white',
            children: [],
          }],
        }
      ],
    },
    {
      id: '06',
      type: 'black',
      children: [{
          id: '07',
          type: 'green',
          children: [],
        },
        {
          id: '08',
          type: 'white',
          children: [{
            id: '09',
            type: 'green',
            children: [],
          }],
        }
      ],
    },
  ]
}

const removeByType = (data, type) => {
  data = data.filter(d => d.type !== type)
  data = data.map(d => {
    d.children = removeByType(d.children, type);
    return d;
  })
  return data
}

console.dir(removeByType(obj.children, 'black'), {
  depth: null
})
mf98qq94

mf98qq942#

我已经做了..

const removeByType = (data, type) =>
  {
  for (let i=data.children.length;--i>=0;)
    {
    if ( data.children[i].type===type)  data.children.splice(i,1)
    else                    removeByType (data.children[i], type)
    }
  return data // just for chaining ... ?
  }

使用方法:
第一个

相关问题