Recursively Search in JSON or Javascript Object

前端 未结 5 769
甜味超标
甜味超标 2020-12-20 07:08

For example:

[{
    id:\'our-purpose\',
    title:\'Our Purpose\',
    slug:\'/our-purpose\',
    backgroundImage:\'images/bg-our-purpose.jpg\',
    showInNa         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-12-20 08:00

    This code builds the nav for you:

    function buildNavForNode(node) {
      var result = "
  • " + node.title + ""; if(node.subpages == undefined) { return result + "
  • "; } else { return result + buildNavForNodes(node.subpages) + "
  • "; } } function buildNavForNodes(nodes) { var result = "
      "; var i = 0; var len = nodes.length; for(; i < len; i++) { result += buildNavForNode(nodes[i]); } return result + "
    "; }

    Here's how you'd use it:

    var testData = [
      {
        id:'our-purpose',
        title:'Our Purpose',
        slug:'/our-purpose',
        backgroundImage:'images/bg-our-purpose.jpg',
        showInNav:1
      },
      {
        id:'our-people',
        title:'Our People',
        slug:'/our-people',
        backgroundImage:'images/bg-our-people.jpg',
        showInNav:1,
        subpages:[
          {
            id:'attorneys',
            title:'Attorneys',
            slug:'/our-people/attorneys',
            subpages:[
              {
                id:'attorneys-cdb',
                title:'Attorneys - Carla DeLoach Bryant',
                slug:'/our-people/attorneys/carla'
              },
              {
                id:'attorneys-jad',
                title:'Attorneys - Jordan A. DeLoach',
                slug:'/our-people/attorneys/jordan'
              },
              {
                id:'attorneys-shh',
                title:'Attorneys - Sarah H. Hayford',
                slug:'/our-people/attorneys/sarah'
              },
              {
                id:'attorneys-jsp',
                title:'Attorneys - Jason S. Palmisano',
                slug:'/our-people/attorneys/jason'
              },
              {
                id:'attorneys-ldw',
                title:'Attorneys - Lindsey DeLoach Wagner',
                slug:'/our-people/attorneys/carla'
              },
            ]
          },
          {
            id:'legal-support',
            title:'Legal Support',
            slug:'/our-people/legal-support',
            subpages:[
              {
                id:'legal-support-tb',
                title:'Legal Support - Theolyn Brock',
                slug:'/our-people/attorneys/theolyn'
              },
              {
                id:'legal-support-cd',
                title:'Legal Support - Cheri DeFries',
                slug:'/our-people/attorneys/cheri'
              },
            ]
          }
        ]
      }
    ];
    
    $(function(){
      htmlToInsert = buildNavForNodes(testData);
      console.log(htmlToInsert);
      $('body').html(htmlToInsert);
    });
    

    You can do this quite readily with a recursive function, but I think this nicely delineates the separation of duties between figuring out what to do with a collection of pages and processing a single page itself.

提交回复
热议问题