Insert months not in the array

2

I am putting a% of the total sales value per month. The code is working perfectly, but I need to array check which months are not in this array and fill it in order 1 to 12. For example:

The code generated an array like this:

  

[{4: 3094}, {6: 9873}, {7: 6531}, {12: 10937}]

Then there are javascript months.

I need the code to insert the other months 4, 6, 7 e 12 in this array with the value of 0 and leave in the order of months that are (1,2,3,5,8,9,10,11)

I have the code:

var resultadosOne = {};
var highchartsOne = [];

for (ano in resultadosOne) {

            var mesVendaArray = [];

            for(mes in resultadosOne[ano]){

                var vendaMes = 0;

                resultadosOne[ano][mes].forEach((venda) => {

                    VendaFormatada = venda.replace(",", ".");

                    if(VendaFormatada <= 0){
                        VendaFormatada = 0;
                    }

                    vendaMes += parseFloat(VendaFormatada);
                });

                mesVendaArray.push({mes:mes, venda: vendaMes});
            };

            if(ano != ""){
                highchartsOne.push({name:ano, data:mesVendaArray, color:'#6AB241'});
            }
        };
    
asked by anonymous 31.10.2016 / 19:14

3 answers

4

Method 1 : To make it work

var mesVendaArray = [{4:3094}, {6:9873}, {7:6531}, {12:10937}];

for (m = 1; m < 12; m++) {            // Para todos os meses possíveis,

  var found = false;

  mesVendaArray.forEach(function(v) { // para todos os itens de mesVendaArray,

    if (!!v[m]){                      // O mês está presente:
      found = true;                   // marque como achado.
    }

  });            

  if (!found) {                       // O mês não foi achado: Crie a entrada.
    var o = {};
    o[m] = 0;
    mesVendaArray.push(o);
  }

}

console.log(mesVendaArray);

However I would not say that this is the best way.

Method 2 : Create an object with multiple properties

Instead of

mesVendaArray.push({mes:mes, venda: vendaMes});

Use

mesVendaObj[mes] = vendaMes;

Benefit: A single object containing the entire result of the month:

mesVendaObj // {4:3094, 6:9873, 7:6531, 12:10937}

You can then start it with

for (m = 1; m < 12; m++) { mesVendaObj[m] = 0 ; }

Before you start summarizing.

    
31.10.2016 / 19:35
3

You can create a for executing from 1 to 12, and check if the iteration number already exists in your ArrayValue, otherwise you have a population of {i: 0}.

Or

You can use a utility like lodash and merge with an array already pre-populated with 0:

var defaultArrayData = {1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0};
var mesVendaData = _.merge(defaultArrayData, mesVendaArray);

link

    
31.10.2016 / 19:26
0

Another way to do this is by filling in the gaps of a second object:

var el = [{4:3094}, {6:9873}, {7:6531}, {12:10937}];
var complete = [{1:0},{2:0},{3:0},{4:0},{5:0},{6:0},{7:0},{8:0},{9:0},{10:0},{11:0},{12:0}];
complete.forEach(function(v, i) {
    if (el[i]) {    
       complete[Object.keys(el[i])] = el[i];
    } 
});

console.log(complete);
    
31.10.2016 / 21:25