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 7637241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:53:15+00:00 2026-05-31T07:53:15+00:00

I have this ugly select list with indents (-) to illustrate hierarchy. I would

  • 0

I have this ugly select list with indents (“-“) to illustrate hierarchy. I would like, using jquery, to get data from source to result.

Source

<option value="12">Subcontractor</option>
<option value="108">-Surveyor</option>
<option value="62">--Excavation works</option>
<option value="554">--Concrete works</option>
<option value="44">-Demolition works</option>
<option value="103">--Site preparation works</option>
<option value="67">--Excavation works</option>
<option value="40">--Contamination works</option>

Example shows 4 levels, but levels count is unknown.

RESULT

var result = {
  "Subcontractor":12 {
    "Surveyor":108 {
      "Excavation works":62,
      "Concrete works":554
    },
    "Demolition works":44 {
      "Site preparation works":103,
      "Excavation works":67,
      "Contamination works":40
    }
  }
};

Notice how option value is added to deepest level only. UPDATE: updated result for eacier code and consistency, parents were missing value, it should be esier to parse to this result.

My jquery skills are average, but I think I could figure this out if someone could suggest direction.

  • 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. Editorial Team
    Editorial Team
    2026-05-31T07:53:16+00:00Added an answer on May 31, 2026 at 7:53 am
    var struct = {}, path = [], indentation = '-';
    
    $('option').each(function (i, el) {
      var
        $el = $(el),
        val = $el.val(),
        label = $el.text(),
        depth = 0,
        parent = struct,
        i;
    
      while (label.substr(0, indentation.length) === indentation) {
        depth += 1;
        label = label.substr(indentation.length);
      }
    
      path = path.slice(0, depth);
      path[depth] = label;
    
      for (i = 0; i < path.length; i += 1) {
        if (typeof parent[path[i]] !== 'object') {
          parent[path[i]] = {};
        }
    
        if (i < path.length - 1) {
          parent = parent[path[i]];
        }
        else {
          parent[path[i]] = val;
        }
      }
    });
    
    console.log(struct);​
    

    http://jsfiddle.net/5zqZQ/4/

    previous version: http://jsfiddle.net/5zqZQ/3/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view like this: CREATE VIEW MyView AS SELECT Column FROM Table
I'm using nHibernate 3.3. I'd like to execute SQL like this: select COUNT(*) from
I have this ugly SELECT statement : Select 'Urbain' as Véhicule, Count( DISTINCT Plaintes.IDEvenements)
I have an network application which uses a select loop like this: bool shutdown=false;
I have this huge ugly query below, i'd like to sort by it in
I have this query SELECT a.* FROM entries a INNER JOIN entries_keywords b ON
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
I have this form: <%= form_tag posts_path, :method => :get, :class => search_nav do
I have this function // add history paths and save data function AddPath( strTag,
I have a perplexing SQL select statement (ugly) that I'm trying to write in

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.