Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
How to render nested data elements using mustache js?

I am using txt.general_1.value in template to display its value but it shows error in console Can't find txt.general_1.value in [object Object]
But if I just use txt in template, it displays [object Object]
Following is the code. Kindly help me in resolving this issue.
Please note that following code is just to reproduce the problem, actual data is different.

<script>
    var json_obj =
            {
            "all": [
                {
                    "row": "row",
                    "column": "col-md-6",
                    "txt": {
                        "general_1": {
                            "type": "a",
                            "value": "aaa - Hello world"
                        },
                        "general_2": {
                            "type": "b",
                            "value": "bbb - Hello world"
                        },
                        "general_3": {
                            "type": "c",
                            "value": "ccc - Hello world"
                        }                       
                    }
                }
            ]
        };
        
        
        var data = json_obj['all'][0];
        var template = "<div class=\"{{row}}\"><p class=\"{{column}}\">{{txt.general_1.value}}</p></div>";
        
        var text = Mustache.render(template, data);        
        $('#target').html(text);    
        
</script>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I tried to reproduce the problem in the code snippet but didn't see any issue. The value is rendered correctly. Check the json_obj in your app, maybe it doesn't always contain the txt.general_1.value properties.

var json_obj = {
  "all": [{
    "row": "row",
    "column": "col-md-6",
    "txt": {
      "general_1": {
        "type": "a",
        "value": "aaa - Hello world"
      },
      "general_2": {
        "type": "b",
        "value": "bbb - Hello world"
      },
      "general_3": {
        "type": "c",
        "value": "ccc - Hello world"
      }
    }
  }]
};


var data = json_obj['all'][0];
var template = "<div class=\"{{row}}\"><p class=\"{{column}}\">{{txt.general_1.value}}</p></div>";

var text = Mustache.render(template, data);
$('#target').html(text);
<script src="https://unpkg.com/mustache@latest"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="target"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!