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

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

刪除嵌套數組中的某一項

刪除嵌套數組中的某一項

翻翻過去那場雪 2018-09-15 14:08:00
[{        "title": "parent",        "expanded": true,        "folder": true,        "id": "0",        "children": [          {            "title": "parent[0]",            "expanded": true,            "folder": true,            "id": "1",            "children": [              {"title": "Books",  "id": "2"},              {"title": "Kindle Books",  "id": "3"},              {"title": "Books For Study",  "id": "4"},              {"title": "Audiobooks",  "id":"5"}            ]          },          {            "title": "parent[1]", "id": "6", "folder": true, "children": [            {"title": "Music", "id": "7"},            {"title": "MP3 Downloads", "id": "8"},            {"title": "Musical Instruments & DJ", "id": "9"},            ]          }        ]      }]想根據id來刪除數組中的某一項,數組長度不確定,是嵌套的,知道用遞歸。。但是不會寫,哪位能幫忙寫下。。
查看完整描述

1 回答

?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

這樣應該就可以了,你試試

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">

function call(arr,tag){

  for(var i = arr.length ; i > 0 ; i--){

      if(arr[i-1].id == tag){

        arr.splice(i-1,1);

      }else{

        if(arr[i-1].children){

          call(arr[i-1].children,tag)

        }

      }

  }

}

var arr = [{

'title': 'parent',

'expanded': true,

'folder': true,

'id': '0',

'children': [

  {

    'title': 'parent[0]',

    'expanded': true,

    'folder': true,

    'id': '1',

    'children': [

      {

        'title': 'Books',

        'id': '2'

      },

      {

        'title': 'Kindle Books',

        'id': '3'

      },

      {

        'title': 'Books For Study',

        'id': '4'

      },

      {

        'title': 'Audiobooks',

        'id': '5'

      }

    ]

  },

  {

    'title': 'parent[1]',

    'id': '6',

    'folder': true,

    'children': [

      {

        'title': 'Music',

        'id': '7'

      },

      {

        'title': 'MP3 Downloads',

        'id': '8'

      },

      {

        'title': 'Musical Instruments & DJ',

        'id': '9'

      },

    ]

  }

]

}];

call(arr,2);
console.dir(arr);

</script>
</body>
</html>


查看完整回答
反對 回復 2018-10-19
  • 1 回答
  • 0 關注
  • 755 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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