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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:49:39+00:00 2026-05-31T05:49:39+00:00

I’ve searched through the myriad parent/child array/object/whatever questions here and elsewhere and haven’t been

  • 0

I’ve searched through the myriad parent/child array/object/whatever questions here and elsewhere and haven’t been able to solve my issue. I know this is a bit long, but I wanted to ensure I’m providing enough info to you guys.

Here’s what I want to do:

  1. I have a number of <div>-delineated items on the page with parent/child relationships, generated via php from my database
  2. I want to use these items as the data source for a D3.js Dendrogram (a node-link tree diagram http://mbostock.github.com/d3/ex/cluster.html)
  3. I’m storing them with left/right nested set values but also parentID values, so I can add ID, parentID, rgt, lft and depth attributes to the <div> elements, so I should have available whatever’s needed to generate the parent/child relationships on the client side
  4. For various reasons, instead of creating a JSON file on the server side to use as the data source, I need to create it on the client side based on the attributes in #3
  5. I’ve had difficulty getting various suggested javascript functions to work and all the D3 examples I’ve found use either a preexisting JSON file or generated math-based file, not attributes of elements already on the page

Here is an example of what already works for me with the D3 Dendrogram, but it’s not generated dynamically:

var tree3 = 
{"sid": "1", "children": [
    {"sid": "2", "children": [
        {"sid": "5", "children": [
            {"sid": "75"},
            {"sid": "85", "children": [
                {"sid": "87"}, ...

To give you an idea of where these attributes are in the DOM, I originally tried the below, but of course it doesn’t generate any hierarchy:

function tree() {
    var tree=[];
    $("article").each(function(){
        tree.push({
            sid:$(this).attr("sid"), 
            l:$(this).attr("l"), 
            r:$(this).attr("r"),
            pid:$(this).attr("pid")
        });
    });
    return tree;
}

I’ve been messing around unsuccessfully with variants of the below to get a nested array:

function tree2() {
   $("article").(function(d) {
       return d.parent().attr("pid") === 0;
}, function(parent, child) {
    return parent.attr("pid") === child.parent().attr("sid");
}).toArray();
}

So, I’m driving myself crazy trying to create the javascript array nested correctly, but it’s dawned on me that I may not need to and that D3’s data selectors and methods could be sufficient. Could you please help me with the code to:

  1. Pull the needed attributes to generate the parent/child relationship within a D3 function (“sid” is the identifier) or, if this isn’t possible,
  2. Create the needed array or array-like object in javascript for use by D3 (still with “sid” as the identifier).

Thanks in advance.

  • 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-31T05:49:40+00:00Added an answer on May 31, 2026 at 5:49 am

    You need to get recursive! Basically the trick is to pass the current parent in as you go, which changes the context and allows you to walk down the tree.

    Update: Working fiddle.

    Assuming your HTML structure is something like this:

    <div sid="1" pid="">
        <div sid="1.1" pid="1">
            <div sid="1.1.1" pid="1.1">
            </div>
        </div>
    </div>
    

    You could do something like this:

    var _json = {};
    
    function addTreeNode(div, parentObj) {
    
        var childObj = {
            sid: $(div).attr("sid"),
            pid: $(div).attr("pid")
        }
    
        // add this to it's parent in the JSON hierarchy
        if (!parentObj.children) parentObj.children = [];
        parentObj.children.push(childObj);
    
        // keep adding for all children div's
        $(div).find("div").each(function() {
            addTreeNode(this, childObj);
        });
    }
    
    // start at the roots, it will magically work it's way out to the leaves
    $("body > div").each(function(){
        addTreeNode(this, _json);
    });
    
    console.log(_json);
    

    Note that if your tree is big enough, you will cause stack overflows, especially in IE. In that case, you’ll need to switch this over from recursion to iteration. It’s not as pretty that way, though.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I am trying to loop through a bunch of documents I have to put
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.