1. 程式人生 > 實用技巧 >JavaScript 陣列遞迴查詢對應的id象返回 陣列 中斷查詢

JavaScript 陣列遞迴查詢對應的id象返回 陣列 中斷查詢

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <script type="text/javascript">
            const fuck = [{"label": "佔用道路問題", "value": 31, "children": [{"label": "經營佔道", "value"
: 35, "children": [{"label": "店外經營佔道", "value": 40, "children": null }, {"label": "流動攤販佔道", "value": 41, "children": null } ] }, {"label": "垃圾佔道", "value": 36, "children": [{"label": "生活垃圾", "value": 42, "children": null }, {"label": "建築垃圾", "value": 43, "children": null }, {"label": "工業垃圾", "value": 44
, "children": null } ] }, {"label": "車輛佔道", "value": 37, "children": [{"label": "機動車佔道", "value": 45, "children": null }, {"label": "非機動車佔道", "value": 46, "children": null } ] }, {"label": "霸佔車位", "value": 38, "children": [] }, {"label": "其他佔道", "value": 39, "children": [] } ]}, {"label": "“兩違”問題", "
value": 32, "children": [{"label": "違法建築", "value": 58, "children": [{"label": "房屋違建", "value": 61, "children": null }, {"label": "小區違建", "value": 62, "children": null }, {"label": "違建棚架", "value": 63, "children": null } ] }, {"label": "違法用地", "value": 59, "children": [] }, {"label": "其他違建", "value": 60, "children": [] } ] }, {"label": "市容設施管理問題", "value": 33, "children": [{"label": "道路損壞", "value": 47, "children": [] }, {"label": "垃圾桶損壞", "value": 48, "children": [] }, {"label": "下水道堵塞", "value": 49, "children": [] }, {"label": "井蓋損壞", "value": 50, "children": [] }, {"label": "路燈損壞", "value": 51, "children": [] }, {"label": "樹木修剪", "value": 52, "children": [] }, {"label": "水電氣", "value": 53, "children": [] }, {"label": "戶外廣告牌", "value": 54, "children": [] }, {"label": "隔音屏損壞", "value": 55, "children": [] }, {"label": "灑水車問題", "value": 56, "children": [] }, {"label": "其他", "value": 57, "children": [] } ] }, {"label": "其他問題", "value": 34, "children": [] } ] console.log(fuck) /** * 深度遞迴搜尋 * @param {Array} arr 你要搜尋的陣列 * @param {Function} condition 回撥函式,必須返回謂詞,判斷是否找到了。會傳入(item, index, level)三個引數 * @param {String} children 子陣列的key */ const deepFind = (arr, condition, children) => { // 即將返回的陣列 let main = [] // 用try方案方便直接中止所有遞迴的程式 try { // 開始輪詢 (function poll(arr, level) { // 如果傳入非陣列 if (!Array.isArray(arr)) return // 遍歷陣列 for (let i = 0; i < arr.length; i++) { // 獲取當前項 const item = arr[i] // 先佔位預設值 main[level] = item // 檢驗是否已經找到了 const isFind = condition && condition(item, i, level) || false // 如果已經找到了 if (isFind) { // 直接丟擲錯誤中斷所有輪詢 throw Error // 如果存在children,那麼深入遞迴 } else if (children && item[children] && item[children].length) { poll(item[children], level + 1) // 如果是最後一個且沒有找到值,那麼通過修改陣列長度來刪除當前項 } else if (i === arr.length - 1) { // 刪除佔位預設值 main.length = main.length - 1 } } })(arr, 0) // 使用try/catch是為了中止所有輪詢中的任務 } catch (err) {} // 返回最終陣列 return main } let myarr = deepFind(fuck, (item, index, level) => item.value === 63, 'children') console.log(20181115092957, myarr) // [{…}, {…}, {…}] console.log(20181115092957, myarr.map(_ => _.value)) // [32, 58, 63] </script> </body> </html>


轉載於:https://www.cnblogs.com/CyLee/p/10193161.html