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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.