Json Merge with Javascript Array

Asked

Viewed 29 times

0

I would like to know (understand) how to treat a json in javascript type:

var arrDados = {"mes":["12","5","1"], "mes":["2","8","3"]};
var meses = {1:'Jan', 12:'Dez', 6:'Junho', 5:'Abril', 3:'Março', 8:'Agosto', 2:'Fevereiro'}

The idea is that according to the MES index and its Array of numbers where each one indicates a month

12 => December 3 => Tides

for (i in arrDados.mes) {
  indice = arrDados.mes[i];
    console.log(indice);
}

So I’m only getting the values of the first json Dice

details:

inserir a descrição da imagem aqui

I have a grid like this: inserir a descrição da imagem aqui

  • arrDados have duplicate keys... that’s invalid syntax. Are you sure JSON is like that? where does JSON come from?

  • Hello @Sergio the previous time I put from that, but I will have several Json like this: {"mes":["12","5","1"]} {"mes":["10","3","2"] but I don’t really know how to do it.. I’ll show you an image

  • 1

    Okay, you mean that variable will be var arrDados = [{"mes":["12","5","1"]}, {"mes":["2","8","3"]}];?

  • yeah, I think that’s the right format

1 answer

1


I believe that in your case you should receive an array of objects, which would look like this...

var arrDados = [{"mes": ["12","5","1"]}, {"mes": ["2","8","3"]}];
var meses = {1:'Jan', 12:'Dez', 6:'Junho', 5:'Abril', 3:'Março', 8:'Agosto', 2:'Fevereiro'};

for (i of arrDados) {
    console.log('..........');
    for (m of i.mes) {
    console.log(meses[m]);
  }
}
console.log('..........');

Browser other questions tagged

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