-
-
Notifications
You must be signed in to change notification settings - Fork 230
New issue
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
获取树对象属性 #43
Comments
|
输入name,找code function getCode(tree, cityName) {
if (tree.name === cityName) {
return tree.code || null;
} else if (tree.children) {
for (var i = 0; i < tree.children.length; i++) {
var code = getCode(tree.children[i], cityName);
if (code) {
return code;
}
}
}
return null;
} |
function getCode(tree, name) {
let res = {}
traversal(tree)
return res
function traversal(node) {
if (!node) return
if (node.name === name) {
res = {...node}
return
}
node.children && node.children.forEach(item => {
traversal(item)
})
}
}
console.log(getCode(tree, '杭州市')) // { name: '杭州市', code: '0571' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: