[英]How to solve this recursion problem(Javascript)
How do you write the above code to achieve my expected results, please help me give some pointers上面的代码怎么写才能达到我的预期效果,请大家帮我指点一下
let apis = {
home: {
index: {
url: '/index',
auth: false
},
tab: {
url: '/tab',
auth: true
}
},
test: {
test: {
url: '/test',
auth: false
}
}
}
let getItem = function (obj){
for(let item in obj){
if(obj.url === '/test') return
if(obj[item] instanceof Object)
getItem(obj[item])
}
return obj
}
console.log(getItem(apis))
Expected results:预期成绩:
{
url: '/test',
auth: false
}
I am doing this now, but it cannot achieve the expected effect我现在在做,但是没有达到预期的效果
const apis = { home: { index: { url: '/index', auth: false }, tab: { url: '/tab', auth: true } }, test: { test: { url: '/test', auth: false } } } const getItem = (obj) => { if (obj.url === '/test') { return obj } for (let item in obj) { if (obj[item] instanceof Object) { const result = getItem(obj[item]) if (result.url === '/test') { return result } } } return {}; } console.log(getItem(apis))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.