亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript - 從嵌套數組中刪除對象

Javascript - 從嵌套數組中刪除對象

青春有我 2023-07-06 18:19:17
我有對象數組,每個對象必須有鍵和標題,但子對象是可選的,并且可以嵌套,我可以多次在子對象中包含子對象。我想通過提供的鍵值(例如鍵 677)刪除某些對象。我嘗試使用過濾器,但我只刪除第一級。也嘗試過遞歸,但不確定我是否做對了。const data = [{    key: '1',    title: 'title 1',    children: [{      key: '098',      title: 'hey',      children: [{        key: '677',        title: 'child'      }]    }]  },  {    key: '123',    title: 'tile 111'  },  {    key: '345',    title: 'something'  }];const rem = '677';const del = (el) => {  if (!el.children) {    return el.key !== rem;  } else {    if (el.key !== rem) {      del(el.children);      return el;    }  }};const res = data.filter((el) => {  return del(el);});console.log(res);
查看完整描述

3 回答

?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

我猜你現有的解決方案就像


const data = [

  {

    key: '1',

    title: 'title 1',

    children: [{

      key: '098',

      title: 'hey',

      children: [{ key: '677', title: 'child'}]

    }]

  },

  { key: '123', title: 'tile 111' },

  { key: '345', title: 'something' }

];


function removeByKey(arr, removingKey){

  return arr.filter( a => a.key !== removingKey);

}

所以它在第一層起作用,但并不深入。


只要改變它就可以完成工作


function removeByKey(arr, removingKey){

  return arr.filter( a => a.key !== removingKey).map( e => {

    return { ...e, children: removeByKey(e.children || [], removingKey)}

  });

}

小警告,對于沒有任何子項的每個項目,子項屬性不會設置為 []。


那么它是如何運作的呢?好吧,我們不是按原樣保留可接受的項目,而是使用與本例{...e}相同的內容來制作副本。{key:e.key, title:e.title, children:e.children}


我們知道用 強制覆蓋子屬性removeByKey(e.children || [], removingKey),因此我們遞歸地調用該方法。該功能的作用并不深入。



查看完整回答
反對 回復 2023-07-06
?
茅侃侃

TA貢獻1842條經驗 獲得超21個贊

我會使用 findIndex 和 splice 的遞歸方法。使用some將允許代碼退出而不運行整個樹。


const data = [{

    key: '1',

    title: 'title 1',

    children: [{

      key: '098',

      title: 'hey',

      children: [{

        key: '677',

        title: 'child'

      }]

    }]

  },

  {

    key: '123',

    title: 'tile 111'

  },

  {

    key: '345',

    title: 'something'

  }

];


const removeKey = (data, key) => {

  // look to see if object exists

  const index = data.findIndex(x => x.key === key);

  if (index > -1) {

    data.splice(index, 1); // remove the object

    return true

  } else {

    // loop over the indexes of the array until we find one with the key

    return data.some(x => {

      if (x.children) {

        return removeKey(x.children, key);

      } else {

        return false;

      }

    })

  }

}


console.log(removeKey(data, '677'))

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


查看完整回答
反對 回復 2023-07-06
?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

您可以使用一些簡單的遞歸來實現這一點:


const data = [

  {

    key: '1',

    title: 'title 1',

    children: [

     {

      key: '098',

      title: 'hey',

      children: [{ key: '677', title: 'child'}]

     }

    ]

  },

  { key: '123', title: 'tile 111' },

  { key: '345', title: 'something' }

];


function removeByKey(key, arr) {

  // loop through all items of array

  for(let i = 0; i < arr.length; i++) {

    // if array item has said key, then remove it

    if(arr[i].key === key) {

      arr.splice(i, 1);

    } else if(typeof(arr[i].children) !== "undefined") {

      // if object doesn't have desired key but has children, call this function 

      // on the children array

      removeByKey(key, arr[i].children);

    }

  }

}


removeByKey('098', data);


console.log(data);

這可能比提供的其他答案更容易理解。


查看完整回答
反對 回復 2023-07-06
  • 3 回答
  • 0 關注
  • 238 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號