記錄常用函數
* * * * *
~~~
/*
* @desc 遞歸函數
* @param {Object} tree: 操作節點
* @param {Array} arr: 要操作的原數組
* @return 返回一個經過操作處理的數組
*/
function convertTree (tree, arr) {
// 首先定義退出條件
if (!tree) return arr
// 需要對當前節點做的操作
// 比如添加屬性或者拉取屬性
arr.push(tree)
// 完成對當前節點操作后,判斷是否有還需要遍歷的條件
// 這里的條件就是當當前節點還有一個需要遍歷的子節點時:
if (tree.children && tree.children.length > 0) {
for (let i = 0; i < tree.children.length; i++) {
convertTree(tree.children[i], arr)
}
}
}
~~~