Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 113347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:41:17+00:00 2026-05-11T02:41:17+00:00

I’ve been pondering this for a while but cannot come up with a working

  • 0

I’ve been pondering this for a while but cannot come up with a working solution. I can’t even psuedo code it…

Say, for example, you have a page with a heading structure like this:

<h1>Heading level 1</h1>      <h2>Sub heading #1</h2>      <h2>Sub heading #2</h2>          <h3>Sub Sub heading</h3>      <h2>Sub heading #3</h2>          <h3>Sub Sub heading #1</h3>          <h3>Sub Sub heading #2</h3>              <h4>Sub Sub Sub heading</h4>      <h2>Sub heading #4</h2>          <h3>Sub Sub heading</h3> 

Using JavaScript (any framework is fine), how would you go about producing a list like this: (with nested lists)

<ol>     <li>Heading level 1         <ol>             <li>Sub heading #1</li>             <li>Sub heading #2                 <ol>                     <li>Sub Sub heading</li>                 </ol>             </li>             <li>Sub heading #3                 <ol>                     <li>Sub Sub heading #1</li>                     <li>Sub Sub heading #2                         <ol>                             <li>Sub Sub Sub heading (h4)</li>                         </ol>                     </li>                 </ol>             </li>             <li>Sub heading #4                 <ol>                     <li>Sub Sub heading</li>                 </ol>             </li>         </ol>     </li> </ol> 

Everytime I try and begin with a certain methodology it ends up getting very bloated.

The solution needs to traverse each heading and put it into its appropriate nested list – I keep repeating this to myself but I can’t sketch out anything!

Even if you have a methodology in your head but haven’t got time to code it up I’d still like to know it! 🙂

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:41:17+00:00Added an answer on May 11, 2026 at 2:41 am

    First, build a tree. Pseudocode (because I’m not fluent in Javascript):

    var headings = array(...); var treeLevels = array(); var treeRoots = array();  foreach(headings as heading) {     if(heading.level == treeLevels.length) {         /* Adjacent siblings. */          if(heading.level == 1) {             treeRoots[] = heading;  // Append.         } else {             treeLevels[treeLevels.length - 2].children[] = heading;  // Add child to parent element.         }          treeLevels[treeLevels.length - 1] = heading;     } else if(heading.level > treeLevels.length) {         /* Child. */          while(heading.level - 1 > treeLevels.length) {             /* Create dummy headings if needed. */             treeLevels[] = new Heading();         }          treeLevels[] = heading;     } else {         /* Child of ancestor. */          treeLevels.remove(heading.level, treeLevels.length - 1);          treeLevels[treeLevels.length - 1].children[] = heading;         treeLevels[] = heading;     } } 

    Next, we transverse it, building the list.

    function buildList(root) {     var li = new LI(root.text);      if(root.children.length) {         var subUl = new UL();         li.children[] = subUl;          foreach(root.children as child) {             subUl.children[] = buildList(child);         }     }      return li;  } 

    Finally, insert the LI returned by buildList into a UL for each treeRoots.

    In jQuery, you can fetch header elements in order as such:

    var headers = $('*').filter(function() {     return this.tagName.match(/h\d/i); }).get(); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 52k
  • Answers 52k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer If it's just 1 variable, I think this is the… May 11, 2026 at 6:37 am
  • added an answer If you're not restricting yourself to a specific platform, there… May 11, 2026 at 6:37 am
  • added an answer I guess what you mean is the -f option, which… May 11, 2026 at 6:37 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.