how to create a data dictionary?


Good I have this in my js, the problem is that in my database every column is a month and I keep the city for example. city: in, column1 (January): 12 etc ..

What I want first is to catch the current month to print the number you have, what occurred to me was to make a data dictionary to check what month we are and so when I browse my array nothing concatenarle, but at the time of printing mes1 marks me indefinitely what am I doing wrong, maybe the dictionary?

 var f=new Date();
 var mes = f.getMonth()+1;

 var mesess = new Array();
      mesess[1] ="en";
      mesess[2] ="fe";
      mesess[3] ="ma";
      mesess[4] ="ab";
      mesess[5] ="may";
      mesess[6] ="ju";
      mesess[7] ="jul";
      mesess[8] ="ag";
      mesess[9] ="se";
      mesess[10] ="oc";
      mesess[11] ="no";
      mesess[12] ="di";


 for(var i=0; i<json.Data.meta.length;i++)



What I want to do is something like this


depending on the number of months that is for example if in my var mes = f.getMonth()+1; is 1 (It is the month of January) then I put mes1=json.Data.meta[i].en; if the month is 2 (is the month of February) then put me mes1=json.Data.meta[i].fe; I do not know if you explain

asked by Juan Jose 21.12.2018 в 17:24

2 answers


Your goal is to get the name of the month based on the month of the column.

const mesNombres= ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio",
  "Julio", "Augusto", "Septiembre", "Octubre", "Noviembre", "Diciembre"

const d = new Date();
document.write('El mes actual es ${mesNombres[d.getMonth()]}');
answered by 21.12.2018 в 18:25

I do not know the content of json.Data.meta as I see it, it must be an array similar to the following:

  {"en": "valor_x", "ciudad": "ciudad_A"},
  {"fe": "valor_x", "ciudad": "ciudad_B"},
  {"ma": "valor_x", "ciudad": "ciudad_C"},
  {"ab": "valor_x", "ciudad": "ciudad_D"},
  {"may": "valor_x", "ciudad": "ciudad_E"}

Based on this, I could say that the content for January is in position 0 and not in position 1 (since it is an array)

// la variable json_data representara el contenido de json.Data.meta
let json_data = [{ "en": "valor_x", "ciudad": "ciudad_A" }, { "fe": "valor_x", "ciudad": "ciudad_B" }, { "ma": "valor_x", "ciudad": "ciudad_C" }, { "ab": "valor_x", "ciudad": "ciudad_D" }, { "may": "valor_x", "ciudad": "ciudad_E" }, { "ju": "valor_x", "ciudad": "ciudad_F" }, { "jul": "valor_x", "ciudad": "ciudad_G" }, { "ag": "valor_x", "ciudad": "ciudad_H" }, { "se": "valor_x", "ciudad": "ciudad_I" }, { "oc": "valor_x", "ciudad": "ciudad_J" }, { "no": "valor_x", "ciudad": "ciudad_K" }, { "di": "valor_x", "ciudad": "ciudad_L" }];

// Tendremos un array de las llaves existentes en json_data deben estar estrictamente 
let llave_meses = ["en", "fe", "ma", "ab", "may", "ju", "jul", "ag", "se", "oc", "no", "di"];

// Obtenemos el mes actual
let mes = new Date().getMonth();

// obtenemos la llave del mes
let llave_mes = llave_meses[mes];

// Sin necesidad de iterar obtenemos el valor del json_data, la prueba es cuando mes = 11
console.log(json_data[mes]) // salida -> { "di": "valor_x", "ciudad": "ciudad_L" }

// El valor de la llave
console.log(json_data[mes][llave_mes]); // salida -> valor_x

// La condicion que esta dentro de tu for
if (json_data[mes].ciudad == 'En') {
    mes1 = json_data[mes][llave_mes];

Since your references of the months (the keys inside your json.Data.meta) are different, to access them from javascript you must treat them as array (the goodness of an object in js), so instead of :


You must use:


I hope I can solve your problem, or give you some idea, since we do not know the structure of json.Data.meta , if you can publish more information or be a little more explicit the community will thank you .

answered by 21.12.2018 в 18:26