cashier_app/commons/utils/dataKit.js

39 lines
782 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 数据 工具类
*
* @author terrfly
* @site https://www.jeepay.vip
* @date 2022/11/30 14:18
*/
const model = {
// 递归遍历树状结构数据 matchFunc 匹配结果, true表示匹配成功 否则继续匹配。
// pnode 可不传入
// 返回结构: [当前数据, 上级数据 ]
recursionTreeData: (treeData, matchFunc, childrenName = 'children', pnode = null ) => {
for (let i = 0; i < treeData.length; i++) {
const item = treeData[i]
// 匹配成功
if(matchFunc(item)){
return [item, pnode]
}
if (item[childrenName] && item[childrenName].length > 0) {
let res = model.recursionTreeData(item[childrenName], matchFunc, childrenName, item)
if(res){
return res
}
}
}
}
}
export default model