English 中文(简体)
如何在联合材料中深入寻找儿童财产组合的阵容
原标题:How to deep find an array for a children property match in JS

让我假设,我有类似的目标(网站旁边厅的可能物体)

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 ))

从一开始,孩子就是一种财产,但成为一阵,因此,我感到困惑的是,这是再次入侵的最佳途径。

问题回答

www.un.org/spanish/ecosoc 您应在other上适用,而不是现在有。 因此:

return group.children ? group.children.some(child => deepFind(child, location))
                      : group.slug === location;

NB: note that you have an extra dash in your search string example, so it wont match.

您的问题是一个小问题,即旁边可能是一阵,我回答说了这一点。

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, target = {}) {
  if (!Array.isArray(group)) group = [group]
  const parseSlug = (o) => o.slug === location
  const result = group.find(item => item.children ? deepFind(item.children, location, target) : parseSlug(item))
  if (!target.item) target.item = result
  return target.item
}
console.log(deepFind(sidebar,  sub1-2-2 ))




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.