javascrpit树(未完)
生活随笔
收集整理的這篇文章主要介紹了
javascrpit树(未完)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
function traverseDf(data, callback) {(function recurse(currentNode) {if(currentNode.children) {for(let item of currentNode.children) {recurse(item);}}callback(currentNode);})(data); }// 廣度優先 function traverseBf(data, callback) {const queue = [];queue.push(data);let currentTree = queue.shift();while(currentTree){if(currentTree.children) {for(const item of currentTree.children) {queue.push(item);}}callback(currentTree);currentTree = queue.shift();} }// 獲取樹中某個key的所有value function getArray(data, key) {const vals = [];const cb = function cb(node) {if(node[key]) {vals.push(node[key]);}}traverseBf(data, cb);return vals }// 給樹中節點添加屬性 function addPropToTree(data, key, callback) {const cb = function cb(node) {node[key] = callback(node);}traverseBf(data, cb);return data; }轉載于:https://my.oschina.net/u/1249401/blog/1570620
總結
以上是生活随笔為你收集整理的javascrpit树(未完)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git传代码到github
- 下一篇: Ubuntu下安装Wine可能导致假死问