让我假设,我有类似的目标(网站旁边厅的可能物体)
const sidebar = {
label: "group 1-1",
children: [
{
label: "group 1-2",
children: [
{
label: sub1-2-1 ,
slug: sub1-2-1
}, {
label: sub1-2-2 ,
slug: sub1-2-2
}
]
},
{
label: sub1-2 ,
slug: sub1-2
}
]
}
Now I need to identify if any of the slug in the array include sub1-2-2
.
I know I could make a recursion, but my test fails and keep falsify. Here s the code I ve written so far:
const sidebar = {
label: "group 1-1",
children: [
{
label: "group 1-2",
children: [
{
label: sub1-2-1 ,
slug: sub1-2-1
}, {
label: sub1-2-2 ,
slug: sub1-2-2
}
]
},
{
label: sub1-2 ,
slug: sub1-2
}
]
}
function deepFind(group, location) {
const parseSlug = (o) => o.slug === location
return group.children ? deepFind(group.children, location) : group.some(parseSlug)
}
console.log(deepFind(sidebar, sub-1-2-2 ))
从一开始,孩子就是一种财产,但成为一阵,因此,我感到困惑的是,这是再次入侵的最佳途径。