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

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

通過另一個數(shù)組 javascript 中的參數(shù)循環(huán)數(shù)組

通過另一個數(shù)組 javascript 中的參數(shù)循環(huán)數(shù)組

眼眸繁星 2024-01-18 10:11:01
我有一系列權(quán)限const permissions = ['search', 'reports', 'whitelist'];我有一個菜單const menus = [  {    text: 'Navigation',    link: 'navigation',    topic: 'all',  },  {    text: 'Dictionary',    link: 'dict',    topic: 'src',  },  {    text: 'Services',    link: '/services',    submenu: [      {        text: 'Check company',        link: '/services/search',        topic: 'search',      },      {        text: 'Report statistic',        link: '/services/reportstatistic',        topic: 'another-topic',      },      {        text: 'Report request',        link: '/services/getrequestreport',        topic: 'another-topic-2',      },      {        text: 'Get whitelist',        link: '/services/getresponsereport',        topic: 'whitelist',      },      {        text: 'Get data',        link: '/services/requestdata',        topic: 'reports',      },    ],  },];我需要過濾menus以檢查數(shù)組的每一項是否topic等于數(shù)組中的一項權(quán)限permissions,如果它具有submenu,則檢查每個項目表單子菜單是否相同或是否topic ==='all'并將其添加到新菜單中。過濾后的預(yù)期結(jié)果是const newArrr = [  {    text: 'Navigation',    link: 'navigation',    topic: 'all',  },  {    text: 'Services',    link: '/services',    submenu: [      {        text: 'Check company',        link: '/services/search',        topic: 'search',      },      {        text: 'Get whitelist',        link: '/services/getresponsereport',        topic: 'whitelist',      },      {        text: 'Get data',        link: '/services/requestdata',        topic: 'reports',      },    ],  },];我試圖制作自己的循環(huán),但它不正確for (let menu of menus) {  for (let permission of filteredPermissions) {    if (menu.topic && (menu.topic == permission || menu.topic === 'all')) {      newArr.push(menu);    }    if (menu.submenu) {      for (let submenu of menu.submenu) {        for (let permissionSecond of filteredPermissions) {          if (!(submenu.topic == permissionSecond) || !(submenu == 'all')) {            menu.submenu.splice(menu.submenu[submenu], 1);          }        }      }    }  }}
查看完整描述

2 回答

?
四季花海

TA貢獻(xiàn)1811條經(jīng)驗 獲得超5個贊

這就是我的做法 - 首先根據(jù).topic權(quán)限是否包含來過濾父數(shù)組,然后迭代具有數(shù)組的結(jié)果submenu并將子數(shù)組屬性重新分配給過濾后的版本:


const permissions = ['search', 'reports', 'whitelist'];

permissions.push('all');


const menus=[{text:"Navigation",link:"navigation",topic:"all"},{text:"Dictionary",link:"dict",topic:"src"},{text:"Services",link:"/services",submenu:[{text:"Check company",link:"/services/search",topic:"search"},{text:"Report statistic",link:"/services/reportstatistic",topic:"another-topic"},{text:"Report request",link:"/services/getrequestreport",topic:"another-topic-2"},{text:"Get whitelist",link:"/services/getresponsereport",topic:"whitelist"},{text:"Get data",link:"/services/requestdata",topic:"reports"}]}];


const filteredMenus = menus.filter(

  item => !item.topic || permissions.includes(item.topic)

);

filteredMenus

  .filter(item => item.submenu)

  .forEach(item => item.submenu = item.submenu.filter(

    subitem => permissions.includes(subitem.topic)

  ));

console.log(filteredMenus);


查看完整回答
反對 回復(fù) 2024-01-18
?
波斯汪

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

您可以使用Array.reduce處理menus數(shù)組,檢查主題是否all包含在 中permissions,并根據(jù)相同的規(guī)則過濾子菜單值:


const permissions = ['search', 'reports', 'whitelist'];


const menus=[{text:"Navigation",link:"navigation",topic:"all"},{text:"Dictionary",link:"dict",topic:"src"},{text:"Services",link:"/services",submenu:[{text:"Check company",link:"/services/search",topic:"search"},{text:"Report statistic",link:"/services/reportstatistic",topic:"another-topic"},{text:"Report request",link:"/services/getrequestreport",topic:"another-topic-2"},{text:"Get whitelist",link:"/services/getresponsereport",topic:"whitelist"},{text:"Get data",link:"/services/requestdata",topic:"reports"}]}];


const includeMenu = (menu) => menu.topic == 'all' || permissions.includes(menu.topic);


const newArr = menus.reduce((c, m) => {

  if (m.submenu) {

    submenus = m.submenu.filter(includeMenu);

    if (submenus.length) {

      c.push({

        text: m.text,

        link: m.link,

        submenu: submenus

      });

    }

  } else if (includeMenu(m)) {

    c.push(m);

  }

  return c;

}, []);


console.log(newArr);


查看完整回答
反對 回復(fù) 2024-01-18
  • 2 回答
  • 0 關(guān)注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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