js 數組的深度遞歸搜索


🙌 👏 🤝 👍

index.js

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": [] } ]

/**
 * 深度遞歸搜索
 * @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]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM