How can I combine two Array into one?

3

I have an array that has the following data:

var mes = {"Mes": ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre']};

and others that have the following data:

var data = [
      {anioTtile: "2018", anioData:[10000,0,20000,0,0,0,0,0,0,0,0,0]},
      {anioTtile: "2017", anioData:[10000,0,20000,0,50000,0,5000,5000,0,0,0,0]},
      {anioTtile: "2016", anioData:[10000,0,0,0,0,0,0,0,0,0,0,0]}
]

And you want this result to be:

[
          ['Mes', '2018', '2017', '2016'],
          ['Enero', 10000, 10000, 10000],
          ['Febrero', 20000, 0, 0],
          ['Marzo', 0, 20000, 0],
          ['Abril', 0, 0, 0],
          ['Mayo', 0, 50000, 0],
          ['Junio', 0, 0, 0],
          ['Julio', 0, 5000, 0],
          ['Agosto', 0, 0, 0],
          ['Septiembre', 0, 0, 0],
          ['Octubre', 0, 0, 0],
          ['Noviembre', 0, 0, 0],
          ['Diciembre', 0, 0, 0]
        ]
    
asked by Andrés Cantillo 18.06.2018 в 21:04
source

1 answer

3

You can do it like this:

var mes = {"Mes": ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre']};

var data = [
      {anioTtile: "2018", anioData:[10000,0,20000,0,0,0,0,0,0,0,0,0]},
      {anioTtile: "2017", anioData:[10000,0,20000,0,50000,0,5000,5000,0,0,0,0]},
      {anioTtile: "2016", anioData:[10000,0,0,0,0,0,0,0,0,0,0,0]}
];

var nuevoArray = [["Mes", "2018", "2017", "2016"]];

mes.Mes.forEach(function(month) {
  nuevoArray.push([month])
});

data.forEach(function(anio) {
  var i = 0;
  anio.anioData.forEach(function(dato) {
    nuevoArray[i + 1].push(dato);
    i++;
  });
});

console.log(nuevoArray);
    
answered by 18.06.2018 / 21:18
source