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

  • Home
  • SEARCH
  • 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 3433578
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:35:36+00:00 2026-05-18T07:35:36+00:00

I have an array(sorted on an attribute called node_id) of ruby objects which basically

  • 0

I have an array(sorted on an attribute called node_id) of ruby objects which basically needs to rendered on the front end as a tree (ul > li> ul > li …). I am using rails 2.3.8. This object is coming from the db, the structure of which might not be easily modifiable.

   [#<Node node_id: 3, children: "10, 42",name: "A", weight: 45, root: true>,
    #<Node node_id: 4, children: "6,88", name: "B", weight: 32, root: true]
    #<Node node_id: 6, children: nil, name: "X", weight: 12, root: false>,
    #<Node node_id: 7, children: "9", name: "P", weight: 98, root: true>,
    #<Node node_id: 9, children: nil, name: "Q", weight: 12, root: false>,
    #<Node node_id: 10, children: "23,56",name: "R", weight: 34, root: false>,
    #<Node node_id: 13, children: nil, name: "T", weight: 75, root: true>,
    #<Node node_id: 23, children: nil, name: "C", weight: 57, root: false>,
    #<Node node_id: 42, children: nil, name: "D", weight: 25, root: false>,
    #<Node node_id: 56, children: nil, name: "Y", weight: 50, root: false>
    #<Node node_id: 88, children: nil, name: "W", weight: 15, root: false>]

What is the best way performance and design wise to render such a tree at the front end? This is just a sample set, the tree is not always balanced and can have hundreds of nodes.

  • 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-18T07:35:36+00:00Added an answer on May 18, 2026 at 7:35 am

    Let’s suppose you convert children into an array of integers with the following

    class Node
      def child_id_array
        case children
        when String: children.scan(/\d+/).map(&:to_i)
        when nil: []
        end
      end
    end
    

    Then, we can index the nodes (for constant time lookup) as follows:

    index=Hash.new
    nodes.each do |x|
      index[x.node_id] = x
    end
    

    Or if you have the facets gem installed, you can just say

    index=nodes.group_by(&:node_id)
    

    Let’s find the root node(s) (the ones that arent a child of anybody else)

    roots = index.keys - nodes.map(&:child_id_array).flatten
    roots.uniq!
    

    Now we render with a recursive function

    def render allnodes, nodenumbers
      puts '<ul>'
      nodenumbers.each do |n|
        puts "<li>#{allnodes[n].name}" 
        render allnodes, allnodes[n].child_id_array
        puts "</li>"
      end
      puts '</ul'>
    end
    

    To kick things off

    render index,roots
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an sorted array which contains first names of people. This array has
I have a custom class in Obj-C called RouteManager which contains an array of
I have an array I've created in JavaScript. The end result comes out to
I have an array which is a list of domains, I want to print
I have an array of objects that I need to sort by a position
I have an Array list of a custom class which is essentially has two
I have a sorted array of points like x=[1 1 1 2 2 4
I have an array of structures, sorted on a member of a structure, something
I have an array of objects: var array = [(id, name, value),(id, name, value)];
I have an array list sorted from oldest to newest. I want to change

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.