0

I have a json file that is requested from a server so i can't modify how is sent, but all the data i need is there, so i want to put that data into the charts, the thing is how i can break down the json file, because the relevant data is nested, the structure is

estados(array of object)

id (property)

name (property)

localidades (array of object)

id (property)

name (property)

type1 (object)

type2 (object) etc..

i need to retrieve the id data from the object estados and localidades then especific data one of the objects nested in localidades.

How i can do that? i only know how to go through every nested object by iteration, but decomposing it?

here is a json sample : http://www.jsoneditoronline.org/?id=9cd4c3af94f14b1cca2d35226794ea78

1 Answer 1

1

The Lodash library (Or Underscore, if you prefer) is what you are looking for. You can use it to manipulate your collections any way you want (pretty much). You could potentially do without helper libraries, but it is much easier (and sometimes faster, though browser dependent [ref]) to do it this way.

Here are some examples in the jsFiddle I put together for a demo. Some example of how to access particular property of an object inside an array of objects:

$scope.findObjects = function () {
    var estado = _.find($scope.allJson, {
            "idEstado": $scope.idToSearch
        });

    $scope.exampleOutput = _.filter(estado.localidades, function (item) {
        return item.demanda && (item.demanda.ata > 5);
    });
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.