Get data from an array of json objects

0

Hi, I'm trying to get the data from an array of json objects and save them in a different javascript array

For examples I get this JSON through an api rest

[
    {
        "temperatura": 2.3,
        "fecha": "2018-02-05T06:13:01.000Z"
    },
    {
        "temperatura": 2.3,
        "fecha": "2018-02-05T06:50:45.000Z"
    },
    {
        "temperatura": 2.3,
        "fecha": "2018-02-05T06:52:14.000Z"
    },
    {
        "temperatura": 29,
        "fecha": "2018-02-05T06:59:37.000Z"
    },
    {
        "temperatura": 30,
        "fecha": "2018-02-05T07:19:36.000Z"
    }
]

I would like to be able to save the temperatures in one array and the dates in another different array

I wanted to do this

$(document).ready(function () {
            var processed_json = new Array();
            var fechas = new Array();
            $.getJSON('https://secure-brushlands-10563.herokuapp.com/sensors/temperaturas', function (json) {
                // Populate series
                console.log(json);

                for (i = 0; i < 11; i++) {
                    $.each(json[i], function (index, data) {
                        processed_json.push(data);
                    });
                }

                console.log(processed_json);
                console.log(typeof(processed_json));

And I get this

    Array [ 2.3, "2018-02-05T05:46:29.000Z", 2.3, "2018-02-05T06:13:01.000Z", 2.3, "2018-02-05T06:50:45.000Z", 2.3, "2018-02-05T06:52:14.000Z", 29, "2018-02-05T06:59:37.000Z", … ]
object 

Thank you very much

    
asked by Emanuel Cortez 07.02.2018 в 02:09
source

1 answer

2

Try running a $.each() to get the result of json , you get the "key" and the "value" and add it to the corresponding array:

$(document).ready(function () {
    var temperaturas = new Array();
    var fechas = new Array();
    $.getJSON('https://secure-brushlands-10563.herokuapp.com/sensors/temperaturas', function (data) {
        $.each(data, function(key, val) {
            temperaturas.push(val.temperatura);
            fechas.push(val.fecha);
        })
    });
    console.log(temperaturas);
    console.log(fechas);
});
    
answered by 07.02.2018 / 03:16
source