Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

195
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda