How to traverse a json and insert it into an array in javascript

0

My json comes like this:

 0:
ciu: "ciudad1"
fe: "2018-01-04 12:04:03"
hu: "46"
id: "62577"
__proto__: Object
1:
ciu: "ciudad2"
fe: "2018-01-02 14:35:16"
hu: "737"
id: "63801"
__proto__: Object
2:
ciu: "ciudad2"
fe: "2018-01-02 14:58:45"
hu: "545"
id: "63824"
__proto__: Object
3:
ciu: "ciudad3"
fe: "2018-01-02 15:35:46"
hu: "33"
id: "63853"

What I want to do is save in an array the cities city1 in one array, city 2 in another array and city3 in another array, after that compare if in the array of city1 the date is between 2018-01-01 a 2018-01-31 save it in an array, this is to check in the city1 in the month of January how many records were filled and so on until December, so on for the other cities, nothing that I have no idea how to do it, some advice?

    
asked by Juan Jose 15.12.2018 в 01:15
source

1 answer

0

To begin with, the pint that has a json like the one you are talking about is this:

[{
        "ciu": "ciudad1",
        "fe": "2018-01-04 12:04:03",
        "hu": "46",
        "id": "62577"
    },
    {
        "ciu": "ciudad2",
        "fe": "2018-01-02 14:35:16",
        "hu": "737",
        "id": "63801"
    },
    {
        "ciu": "ciudad2",
        "fe": "2018-01-02 14:58:45",
        "hu": "545",
        "id": "63824"
    },
    {
        "ciu": "ciudad3",
        "fe": "2018-01-02 15:35:46",
        "hu": "33",
        "id": "63853"
    }
]

I will describe how to proceed:

  • 1st You bring the json through an AJAX call
  • 2nd You convert the JSON into an object using JSON.parse

  • 3º You go through the object capturing the values you want and putting them into the arrays you want

Step 1: Bring the JSON (Step 2 is explained in the comments of the code) I guess the JSON will bring it to you after making an AJAX call:

function getCitiesData(){
 req=new XMLHttpRequest();
req.onreadystatechange=function(){ 
  if (req.readyState === 4 && req.status === 200){ // readyState === 4 significa que está completo y el status === 200 significa que está OK
  jsonparsed = JSON.parse(req.responseText); //Paso 2: Conviertes el JSON en un objeto através de JSON.parse. No te olvides de declarar previamente la variable jsonparsed. 
  } else if (req.readyState === 4 && req.status === 404){
    console.log(req.responseText); //Para ver el error en el caso de que no haya podido traerse el JSON
  }

};
req.open("GET",'url-a-la-que-haces-tu-llamada-ajax',true);
req.send();
}

STEP 3 You scroll the object ( jsonparsed )

Option A

  jsonparsed.forEach(function(objeto){
  arrayCiudades.push(objeto.ciu);
  arrayFe.push(objeto.fe);
  arrayHu.push(objeto.hu);
  arrayId.push(objeto.id);
     }
  }); 
 }

Option B (I think this fits better with the one you mention to me)

  var arrayDeArrays = [];
  var fila = 0
  jsonparsed.forEach(function(objeto){
  arrayDeArrays[fila] = [];
  arrayDeArrays[fila].push(objeto.ciu);
  arrayDeArrays[fila].push(objeto.fe);
  arrayDeArrays[fila].push(objeto.hu);
  arrayDeArrays[fila].push(objeto.id);
  fila +=1;
     }
  }); 
 }

You already have your data in arrays instead of having it in an array of objects. Is that what you wanted, right? Then you want:

  

after that, compare if in the array of city1 the date is between   2018-01-01 to 2018-01-31 save it in an array, this is to check in   the city1 in the month of January how many records were filled and so on   until December, and so on for the other cities

Well, I'll tell you that arrayDeArrays (Option B) is like a stack of arrays
arrayDeArrays [0] [0] gives you the name of the city of the first array on the stack.
arrayDeArrays [0] [1] gives you the date of the city of the first array of the stack.
arrayDeArrays [0] [2] gives you the "hu" of the city of the first array of the stack.
arrayDeArrays [0] [3] gives you the city id of the first array of the stack.

The truth is that I do not have more time for today to devote to this, I'm sorry ..

    
answered by 15.12.2018 в 03:27