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

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

如何解析 JSON 樹并檢查名稱在整個樹結構中是否唯一?

如何解析 JSON 樹并檢查名稱在整個樹結構中是否唯一?

瀟瀟雨雨 2023-11-02 21:22:12
我有這棵樹,它可以比這棵樹更深,我需要確保每個單位、實體和投資組合的名稱在整個樹中都是唯一的。誰能幫我用 JS (ES6) 實現(xiàn)解析這棵樹并檢查名稱的唯一性?非常感謝!reduxTree: {    units: [      {        name: 'ROOT',        units: [          {            name: 'ORG1',            units: [],            entities: [              {                name: 'LE1',                portfolios: [                  {                    name: 'PO1',                   }                ]              }            ],          },          {            name: 'ORG2',            units: [],            entities: [              {                name: 'LE2',                portfolios: [                  {                    name: 'PO2',                  }                ]              }            ],          }        ],        entities: [          {            name: 'LE3',            portfolios: [              {                name: 'PO3',              }            ]          }        ],      }    ]  }
查看完整描述

3 回答

?
RISEBY

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

這是一種遞歸方法,我檢查所有名稱,如果一個名稱出現(xiàn)兩次,我就中斷迭代


const reduxTree = { units: [{ name: 'ROOT', units: [{ name: 'ORG1', units: [], entities: [{ name: 'LE1', portfolios: [{ name: 'PO1', }] }], }, { name: 'ORG2', units: [], entities: [{ name: 'LE2', portfolios: [{ name: 'PO2', }] }], } ], entities: [{ name: 'LE3', portfolios: [{ name: 'PO3', }] }], }] }; 

  

const mySet = new Set();

function validateTree(obj){

   if(!obj || obj.length == 0){

   return true;

   }

   if(mySet.has(obj[0].name)){

      return false;   

   } else {

     return validateTree(obj[0].units) && validateTree(obj[0].entities) && validateTree(obj[0].portfolios)

   }

}


console.log(validateTree(reduxTree.units))


查看完整回答
反對 回復 2023-11-02
?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

您可以對收集可見名稱的 a 進行閉包,并對要分離的對象和對象的其余部分Set進行解構。name


檢查名稱后,如果在集合中,則返回 false,否則將名稱添加到集合中并迭代并檢查其余屬性。


const

    hasUniqueNames = (object, names = new Set) => {

        const c = ({name, ...rest }) => {

            if (name !== undefined) if (names.has(name)) return false;

            names.add(name);

            return Object.values(rest).every(v => !v || typeof v !== 'object' || c(v));

        };


        return c(object);

    },

    reduxTree = { units: [{ name: 'ROOT', units: [{ name: 'ORG1', units: [], entities: [{ name: 'LE1', portfolios: [{ name: 'PO1' }] }] }, { name: 'ORG2', units: [], entities: [{ name: 'LE2', portfolios: [{ name: 'PO2' }] }] }], entities: [{ name: 'LE3', portfolios: [{ name: 'PO3' }] }] }] },

    result = hasUniqueNames(reduxTree);


console.log(hasUniqueNames(reduxTree));


查看完整回答
反對 回復 2023-11-02
?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

嘗試一下這個


我提取所有"name":"<name>"并使用 Set 來查看它們是否是唯一的


const reduxTree = { units: [{ name: 'ROOT', units: [{ name: 'ORG1', units: [], entities: [{ name: 'LE1', portfolios: [{ name: 'PO1', }] }], }, { name: 'ORG2', units: [], entities: [{ name: 'LE2', portfolios: [{ name: 'PO2', }] }], } ], entities: [{ name: 'LE3', portfolios: [{ name: 'PO3', }] }], }] } 


const names = [...JSON.stringify(reduxTree).matchAll(/"name":"(\w+)"/g)].map(m => m[1])

const unique = new Set(names).size === names.length


console.log("Unique?",unique,names.join(","))


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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