generating an array of [month/year] p/ send to an ajax

Asked

Viewed 314 times

1

I’m not sure if I specified the title correctly, but, come on, I have a url that returns a json with a bank value, the value comes this way: "01/2010" until "12/2016". I created a loop that brings me this way: val['01/2010'].

this is the json:

<code>inserir a descrição da imagem aqui</code>

I would like a way to not have to write these dates directly in the code, but to generate dynamically and put inside my loop. follow the fiddle: https://jsfiddle.net/xey416mp/

  • From what I understand by "val['01/2010']" then '01/2010' is the key and not the value

  • @Miguel is just like that

  • want the key so, how is your loop? By the code that showed what to use there is the key

  • Format your Json, with something like [&#xA; {"Data":"01/2010"},&#xA; {"Data":"02/2010"},&#xA; {"Data":"03/2010"}&#xA;]

2 answers

3

Leandro, to go through a json, just do so.:

var data = {
  CURSO: "",
  SITUACAO: "",
  TOTAL_FINANCIAMENTO: "",
  SEMESTRES_FINANCIADOS: "",
  VALOR_FINANCIADO_SEM: "",
  DATA_REPASSE: "",
  '01/2010': 0.00,
  '02/2010': 0.00,
  '03/2010': 0.00,
  '04/2010': 0.00,
  '05/2010': 0.00,
  '06/2010': 0.00,
  '07/2010': 0.00,
  '08/2010': 0.00,
  '09/2010': 0.00,
  '10/2010': 0.00,
  '11/2010': 0.00,
  '12/2010': 0.00
};

var isDate = function (text) {
  var date = new Date("01/" + text);
  return date && !isNaN(date) && date != "Invalid Date"
}

for (var key in data) {
  var value = data[key];
  if (isDate(key)) {
    console.log(key, value);
  }
}

Note that in your specific case, in addition to going through the object keys, you should check whether the key represents a date.

2

Just like @Tobymosque said, so if you want to iterate dates sequentially, you can do so:

var dates = {};

for (var key in val) {
    var index = key.indexOf("/"),
        year = key.substr(index + 1);
    /* checa se o ano já não foi definido e
       checa se key aparenta ser uma data */
    if (!dates[year] && index !== -1)
        dates[year] = {};
}

for (key in val) {
    var index = key.indexOf("/");
    /* checa se key aparenta ser uma data */
    if (index !== -1)
        dates[key.substr(index + 1)][key.substr(0, index)] = val[key];
}

Now you can iterate the array normally dates and concatenate the data of the months:

var html = "";
for (var year of dates) {
    for (var month of year) {
        html += '<td align="right">' + month + '</td>';
    }
}

$('#resultAluno tbody').append('<tr><td>' +
    val.CURSO + '</td> <td>' +
    val.SITUACAO + '</td><td align="right">' +
    val.TOTAL_FINANCIAMENTO + '</td><td align="right">' +
    val.SEMESTRES_FINANCIADOS + '</td><td align="right">' +
    val.DATA_REPASSE + '</td>' + html + "</tr>"
);
  • sorry for the late reply, I was in a bit of a jam, but back to the subject, that for() I’ll put inside my $.each? as jsfiddle already passed?

  • @Leandro Yes, in the scope where it has val.

  • is giving error in the line where was left a ")" where has the variable html +=

  • @Leandro It was bad! I put that ) unintentionally...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.