代码之家  ›  专栏  ›  技术社区  ›  milan

如何找到与提供的slug匹配的特定对象的索引

  •  0
  • milan  · 技术社区  · 6 年前

    我有以下数据结构。它是一个嵌套很深的对象。

    根据下面的数据结构,我需要找到一个对象的索引,该对象的slug为 level-1.1.3 因此,我可以知道如果提供给我这个slug,我就必须在这个object子数组中添加项。如果没有找到任何弹头,那么在顶部添加,如 Another Level 1 B 对象。

    我尝试了下面的方法,但我没有这个有效的方法,也找不到索引,但找到了整个对象。问题是,它也会转到else块,因此如果我从else块返回null或其他内容,那么如果对象的级别太深,则对象中包含的slug将找不到。

    const data = [{
        title: "Level 1 A",
        slug: "level-1-A",
        url: "url",
        children: [{
          expanded: true,
          title: "Level 1.1",
          slug: "level-1.1",
        }]
      },
      {
        title: "Another Level 1 B",
        slug: "another-level-1-b",
        url: "url",
        children: []
      },
      {
        title: "Level 1 C",
        slug: "level-1-c",
        expanded: true,
        children: [{
          expanded: true,
          title: "Level 1.1",
          slug: "level-1.1",
          children: [{
            title: "Level 1.1.1",
            slug: "level-1.1.1",
            url: "/child",
            children: [{
              title: "Level 1.1.2",
              slug: "level-1.1.2",
              url: "/",
              children: [{
                title: "Level 1.1.3",
                slug: "level-1.1.3",
                url: "/"
              }]
            }]
          }]
        }]
      }
    ];
    
    
    function findIndex(data, slug) {
      var index = 0;
      for (var i = 0; i < data.length; i++) {
        if (data[i].slug === slug) {
          console.log('found in depth', data[i]);
          break;
        } else if (data[i].children && data[i].children.length > 0) {
          console.log('did not find in first level')
          findIndex(data[i].children, slug)
        } else {
          console.log('did not find at all')
        }
      }
    }
    
    
    findIndex(data, slug = "level-1.1.3")
    1 回复  |  直到 6 年前
        1
  •  1
  •   protoproto    6 年前

    您可以在其中使用array find和recursive来查找slug(或其他任何内容):

    var data = [
      {
        title: "Level 1 A",
        slug: "level-1-A",
        url: "url",
        children: [
          {
            expanded: true,
            title: "Level 1.1",
            slug: "level-1.1",
          }
        ]
      },
      {
        title: "Another Level 1 B",
        slug: "another-level-1-b",
        url: "url",
        children: []
      },
      {
        title: "Level 1 C",
        slug: "level-1-c",
        expanded: true,
        children: [
          {
            expanded: true,
            title: "Level 1.1",
            slug: "level-1.1",
            children: [
              {
                title: "Level 1.1.1",
                slug: "level-1.1.1",
                url: "/child",
                children: [
                  {
                    title: "Level 1.1.2",
                    slug: "level-1.1.2",
                    url: "/",
                    children: [
                      { title: "Level 1.1.3", slug: "level-1.1.3", url: "/" }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ];
    
    function findslug(title){
      var slug = "";
      data.find(function f(current){
        if(current.title === title){
          slug = current.slug;
          return current;
        };
        if(current.children)
          return current.children.find(f);
      });
      return slug;
    }
    
    console.log(findslug("Level 1.1.3"));
    console.log(findslug("Level 1 C"));