-
Notifications
You must be signed in to change notification settings - Fork 139
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
jrg interview questions #42
Comments
题目1: (比较笨的方法。)
|
题目1-解答:
|
题目2-解答:
|
题目一
|
这样写有问题,首先只能解决一层嵌套,‘五道口’第二层嵌套就拿不到了。 其次, function tree(list){
const firstArray = list.filter(o=>{if(o.parentCode=='') return o})
const func = (arr) => {
arr.map(o=>{
let children = []
list.forEach((e)=>{
if(e.parentCode === o.code){
children.push(e)
func(children)
}
})
if(children.length) o.children = children
})
return arr
}
return func(firstArray)
} |
题一
|
function tree(array) {
const result = array.filter(r => r.parentCode === '');
(function loop(rootNodes) {
for (const rootNode of rootNodes) {
rootNode.children = array.filter(r => r.parentCode === rootNode.code);
if (rootNode.children.length) {
loop(rootNode.children);
}
}
})(result);
return result;
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
题目1
写一个函数tree,实现如下功能
题目2
补充如下代码实现一个简单的jQuery
The text was updated successfully, but these errors were encountered: