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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:20:10+00:00 2026-06-09T03:20:10+00:00

needing some advice on how to do this properly recursively. Basically what I’m doing,

  • 0

needing some advice on how to do this properly recursively.

Basically what I’m doing, is entering in a bunch of text and it returns it as JSON.

For example:

The text:

q
b
name:rawr

Returns:

[
  "q",
  "b",
  {
    "name": "rawr"
  }
]

And the following input:

q
b
name:rawr:awesome

Would return (output format is not important):

[
  "q",
  "b",
  {
    "name": {
        "rawr": "awesome"
    }
  }
]

How can I modify the following code to allow a recursive way to have objects in objects.

var jsonify = function(input){
  var listItems = input, myArray = [], end = [], i, item;

  var items = listItems.split('\r\n');

  // Loop through all the items
  for(i = 0; i < items.length; i++){

    item = items[i].split(':');

    // If there is a value, then split it to create an object
    if(item[1] !== undefined){
      var obj = {};
      obj[item[0]] = item[1];  
      end.push(obj);
    }
    else{
      end.push(item[0]);
    }
  }

  // return the results
  return end;
};
  • 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-09T03:20:12+00:00Added an answer on June 9, 2026 at 3:20 am

    I don’t think recursion is the right approach here, a loop could do that as well:

    var itemparts = items[i].split(':');
    
    var value = itemparts.pop();
    while (itemparts.length) {
        var obj = {};
        obj[itemparts.pop()] = value;
        value = obj;
    }
    end.push(value);
    

    Of course, as recursion and loops have equivalent might, you can do the same with a recursive function:

    function recurse(parts) {
        if (parts.length == 1)
            return parts[0];
        // else
        var obj = {};
        obj[parts.shift()] = recurse(parts);
        return obj;
    }
    end.push(recurse(items[i].split(':')));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for some style advice for testing this piece of (Objective-)C++ code, I
I'll be building a Wordpress theme and would be needing some advice in implementing
Needing some guidance about how to properly graph data that is very small and
I'm needing some advice on structuring my classes for an application which models 6
I'm having some IE8 javascript issues (joy). Basically, I'm needing to convert any negative
I strongly suspect that this boils down to me needing to understand some language
I am needing some information on including files in PHP classes. E.G. include Foo2.php;
I am needing some help with SQL syntax. Say I have a members table,
I´m new to Play and Scala world, so I´m needing some help to figure
I am needing to transfer some logs which were timestamped in ticks to an

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.