JQuery - How to know how many elements are in a JSON file

2

I have my "pck.json"

{
    "Datos":
    [
        {
            "Nombre":"Alekz"
        },
        {
            "Nombre":"Andrea"
        },
        {
            "Nombre":"Janny"
        },
        {
            "Nombre":"Haytham"
        }
    ],
    "Datos2":
    [
        {
            "Nombre":"Alekz"
        },
        {
            "Nombre":"Andrea"
        },
        {
            "Nombre":"Janny"
        },
        {
            "Nombre":"Haytham"
        }
    ],
    "Datos3":
    [
        {
            "Nombre":"Alekz"
        },
        {
            "Nombre":"Andrea"
        },
        {
            "Nombre":"Janny"
        },
        {
            "Nombre":"Haytham"
        }
    ]
}

In JQuery I have

jQuery(document).ready(function($) {
    var obj = $.getJSON('pck.json',function(data){
        var name = data.Datos;
        var cont = 1;
        for (var i = 0; i < name.length; i++) {
            var object = name[i];
            //alert(name.length);
            for (property in object) {
                var valor = object[property];
                $(".unatabla").append("<tr><td>"+cont+"</td><td>"+valor+"</td></tr>");
                cont+=1;
            }
        }
    });
});

In what way can I count the elements inside the JSON, that is, I get the result 3 because in the JSON are "Data", "Data2", and "Data3". Please, thank you.

    
asked by Máxima Alekz 04.08.2016 в 16:32
source

1 answer

4

Obtaining the length property of the "keys" of the object, can be done with pure javascript (vanilla):

Object.keys(data).length;

For references on this:

link

    
answered by 04.08.2016 / 16:35
source