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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:12:15+00:00 2026-06-01T10:12:15+00:00

Given the following array of json objects: var items = [ {id:1, parentId:0, name:’item1′},

  • 0

Given the following array of json objects:

var items = [
    {id:1, parentId:0, name:'item1'},
    {id:2, parentId:1, name:'item1.2'},
    {id:3, parentId:1, name:'item1.3'},
    {id:4, parentId:3, name:'item3.4'},
    {id:5, parentId:3, name:'item3.5'},
    ...more nesting, levels, etc.
];

And a base object like:

var myObj = function(id, parentId, name, children){
    this.id = id;
    this.parentId = parentId;
    this.name = name;
    this.children = children;
};

how do I use recursion to loop through this items array and build a new array like so:

var newArray = [
    new myObj(1, 0, 'item1', [
        new myObj(2, 1, 'item1.2', []),
        new myObj(3, 1, 'item1.3', [
            new myObj(4, 3, 'item3.4', []),
            new myObj(5, 3, 'item3.5', [])
        ])
    ]);
];

any help is greatly appreciated

edit:
the parent/child relationship can be infinite. so i’m looking for a recursion function that would convert the first “items” array into the second “newarray”

  • 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-06-01T10:12:17+00:00Added an answer on June 1, 2026 at 10:12 am

    I don’t know if this helps, but in this fiddle there’s an iterative solution that creates that newArray.
    Using a json obj as an array and then adding the children as you iterate over the items array.

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

Sidebar

Related Questions

Given the following PHP array: Array ( [0] => Array ( [name] => Restaurant
Given the following array: var things = ['sandwich', 17, 'fake@sample.com', 3, 'horse', 'octothorpe', 'anotheremail@sample.com',
// given following array: $data = array( 0=>array( data=>object1, col=>array( 0=>array( data=>object2, col=>array( 0=>array(
Given following Ruby statements: (Read input and store each word in array removing spaces
I'm using the following function to get a JSON Array from a php webpage.
I need to create an array which will give me the following json encoded
Given the following array array( 0 => array( 'id' => '54'), 1 => array(
with reference of this question Sort json array I have the following JSON String
Given the following NumPy array, > a = array([[1, 2, 3, 4, 5], [1,
ok...in php how do i do this? given the following scenario: // array of

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.