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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:49:23+00:00 2026-05-26T09:49:23+00:00

I have a 2D array representing a tree in this format: [[Food, 0], [Dairy,

  • 0

I have a 2D array representing a tree in this format:

[["Food", 0], ["Dairy", 1], ["Milk", 2], ["Low-fat", 3], ["Butter", 2], ["Cheese", 2], ["Vegetables", 1], ["Spinach", 2], ["Meat", 1], ["Fish", 2], ["Salmon", 3], ["Poultry", 2]]

Each item (node) is an array where its first element is the name, and the second is the level (depth) of the node.

I need to convert this 2D array to nested JavaScript objects, where each node-object consists of name (string) and children (array of objects). The result should be the root object (here, “Food”) with all other items as its children. The input array will always come ordered, so it can be assumed that the first element is root.

What’s the best way to go about this, either with iteration or recursion?

  • 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-26T09:49:23+00:00Added an answer on May 26, 2026 at 9:49 am

    Recursion isn’t necessary. I think something like this is what you’re looking for:

    function tree_from_list(list) {
        var list_node = list.shift();
        var depth = list_node[1];
        var root = {name: list_node[0], children: []};
        var tree_node;
        var cur_nodes = []; 
        cur_nodes[depth] = root;
        while (list.length > 0) {
            list_node = list.shift();
            tree_node = {name: list_node[0], children: []};
            depth = list_node[1];
            if (cur_nodes[depth - 1] === undefined)
                throw 'invalid list!';
            cur_nodes[depth - 1].children.push(tree_node);
            cur_nodes[depth] = tree_node;
        }
        return root;
    }
    var list = [["Food", 0], ["Dairy", 1], ["Milk", 2], ["Low-fat", 3], ["Butter", 2], ["Cheese", 2], ["Vegetables", 1], ["Spinach", 2], ["Meat", 1], ["Fish", 2], ["Salmon", 3], ["Poultry", 2]];
    var tree = tree_from_list(list);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of bytes representing an image in Windows BMP format and
I have an array of ushorts, with each ushort representing a 12-bit word. This
I have an MxN array of ints representing colors (say RGBA format, but that
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
Currently, I'm working on representing an AST tree I have, written in SML, in
I have a C++ class representing a hierarchically organised data tree which is very
have an array of values [1,2,4] representing the values of a multi-select box, how
I have a byte array representing a picture. I want to present the picture
I am writing a tic-tac-toe game in Java. I have a 2D array representing
I have an unmanaged 2d array ( double A** ) representing a matrix. Each

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.