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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:36:38+00:00 2026-06-17T05:36:38+00:00

Say I have a json object with nested arrays to unknown depths. I want

  • 0

Say I have a json object with nested arrays to unknown depths. I want to feed each array into a _.template function. For Example, my json object might look like:

$start_elements = array (
        array(
            "elementTag"=>"li",
            "elementClass"=>"dashboard",
            "elementContent"=>array(
                "elementTag"=>"a",
                "elementContent"=>"Dashboard",
                "href"=>"#home"
                ) 
        ),
        array(
            "elementTag"=>"li", 
            "elementClass"=>"count indicator", 
            "elementContent"=>array(
                array(
                    "elementTag"=>"span", 
                    "elementClass"=>"data-count='8'", 
                    "elementContent"=>"Notifications"
                ), 
                array(
                    "elementTag"=>"ul", 
                    "elementClass"=>" ",
                    "elementContent"=>array(
                        "elementTag"=>"li", 
                        "elementContent"=>array(
                            "elementTag"=>"a",
                            "href"=>"#", 
                            "elementExtra"=>"data-modal",
                            "elementContent"=>array(
                                array(
                                    "elementTag"=>"h4",
                                    "elementContent"=>"Lorem Ipsum"
                                    ), 
                                array(
                                    "elementTag"=>"<p>",
                                    "elementContent"=>"Lorem ipsum dolor sit imet smd ddm lksdm lkdsm"
                                )
                            )
                        )
                    )
                )
            )
        )
);
json_encode($start_elements);

_.template:

_.template('<<%= elementTag %> class="<%= elementClass %>" href="<%= href %>"><%= elementContent %></<%= elementTag %>')

The nested structure of the arrays is significant because I want to output the html in the same nested structure. For example, the above object would output an li object with an anchor tag inside of it. How do I go about applying the template to each nested array, while preserving this structure?

  • 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-17T05:36:40+00:00Added an answer on June 17, 2026 at 5:36 am

    I’d make a wrapper function around your template to keep your template logicless. Then you can use underscore’s utility methods to check if you have an array, object (or string) (_.isArray and _.isObject) in your hands.

    You’ll need to pass in your wrapper function into the template so you can use it. You can just _.extend your data with {tmpl: tmpl}.

    Here’s a working, though simplified, example:

        var data = {
        elementTag: "li",
        elementContent: [{
            elementTag: "a",
            elementContent: "Dashboard"
        }, {
            elementTag: "div",
            elementContent: "Hello"
        }]
    };
    
    var tmpl = _.template('<<%= elementTag %>><%= template(elementContent) %></<%= elementTag %>>');
    
    function template(elData) {
        var html = "";
        if (_.isArray(elData)) {
            _.each(elData, function (el) {
                html += template(el);
            });
        } else if (_.isObject(elData)) {
            html = tmpl(_.extend({
                template: template
            }, elData));
        } else {
            html = elData;
        }
        return html;
    }
    
    console.log(template(data));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have some nested json objects. And you want to retrieve some
For example lets say I have a JSON object that contains states and cities.
say I have the following json object; {'fname':'john', 'lname':'Locke'} and the following text boxes
I have this application where I read a sequence of JSON object ... say,
I have a json object(say dobject) returning data from a json file Here is
Explanation: Let's say I have an object graph that's nested several levels deep and
Let's say I have a JSON Object that is this: {success:true, uploaded_url:uploaded_url} How do
Let's say I have a JSON object like var myjson = { com.mycompany.top.Element :
I have a JSON object, say: { foo: { bar: 1 }, baz: 2
Lets say I have a JSON document / JavaScript object that looks like this:

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.