We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
/** * 深度递归搜索 * @param {Array} arr 你要搜索的数组 * @param {Function} condition 回调函数,必须返回谓词,判断是否找到了。会传入item参数 * @param {String} children 子数组的key */ export function deepFind(arr, condition, children) { // 即将返回的数组 const main = [] // 开始轮询 ;(function poll(arr) { // 如果传入非数组 if (!Array.isArray(arr)) return // 遍历数组 for (let i = 0; i < arr.length; i++) { // 获取当前项 const item = arr[i] // 检验是否已经找到了 const isFind = condition && condition(item) || false // 如果已经找到了 if (isFind) { main.push(item) // 如果存在children,那么深入递归 } else if (children && item[children] && item[children].length) { poll(item[children]) } } })(arr, 0) // 返回最终数组 return main } // example 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': 63, 'children': [] }, { 'label': '户外广告牌', 'value': 54, 'children': [] }, { 'label': '隔音屏损坏', 'value': 55, 'children': [] }, { 'label': '洒水车问题', 'value': 56, 'children': [] }, { 'label': '其他', 'value': 57, 'children': [] } ] }, { 'label': '其他问题', 'value': 34, 'children': [] } ] const arr = deepFind(fuck, item => item.value === 63, 'children') console.log(20181115092957, arr) // [{"label":"违建棚架","value":63,"children":null},{"label":"水电气","value":63,"children":[]}]
The text was updated successfully, but these errors were encountered:
No branches or pull requests
The text was updated successfully, but these errors were encountered: