第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

像這種數(shù)據(jù)格式如何遞歸

像這種數(shù)據(jù)格式如何遞歸

慕哥9229398 2019-03-12 16:13:41
let arr = [{    role: 'admin',    left: 'fzz',    children: [{        role: 'other',        right: 'pdd',        children: [{            role: 'admin'        }]    }] }]像這種數(shù)據(jù)格式如何遞歸返回一個(gè)數(shù)組,數(shù)據(jù)格式保持不變,但是取出里面role為admin的呢?(包括children里面的數(shù)據(jù)也要對role進(jìn)行篩選)如果父級role不是admin,則該級和它的children都丟棄返回:arr = [{    role: 'admin',    left: 'fzz',    children: [{        role: 'admin',        right: 'pdd'    }]}]
查看完整描述

2 回答

?
人到中年有點(diǎn)甜

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊

這需求改的和之前差的很大哦...

這個(gè)答案是應(yīng)之前的需求:返回所有admin,并且刪除children中admin。


getRoles(getData());


function getRoles(data, role = 'admin') {

  let resArr = [];


  main(data);


  return resArr;


  function main(data) {

    if (data && data.length) {

      data.forEach((d, i) => {

        if (d.role === 'admin') resArr.push(data.splice(i, 1));

        if (d.children && d.children.length) main(d.children);

      });

    }

  }

}


function getData() {

  return [{

    role: 'other',

    children: [{

      role: 'admin',

      index: '1'

    }, {

      role: 'other'

    }]

  },{

    role: 'admin',

    index: '2',

    children: [{

      role: 'other',

      children: [{

        role: 'admin',

        index: '3'

      }]

    }]

  }];

}


查看完整回答
反對 回復(fù) 2019-03-29
?
溫溫醬

TA貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊

let arr = [{

    role: 'admin',

    left: 'fzz',

    children: [{

        role: 'other',

        right: 'pdd',

        children: [{

            role: 'admin'

        }]

    }] 

}]

arr.find(function(x){

    return x.role ==='admin';

})


查看完整回答
反對 回復(fù) 2019-03-29
  • 2 回答
  • 0 關(guān)注
  • 426 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)