Insert non-existent months into the array

Asked

Viewed 88 times

2

I’m putting in a array total sales value per month. The code is working perfectly, but I need the javascript 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 you have the months 4, 6, 7 e 12.

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

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'});
            }
        };

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 wouldn’t say this is the best way.

Method 2: Create an object with multiple properties

Instead of

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

Utilize

mesVendaObj[mes] = vendaMes;

Benefit: A single object containing the entire month’s result:

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

You can then incialize it with

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

Before starting the summary.

  • I’m trying to use his Method 1 and he doubles his months. For example, if you have 5 months with the sale values it takes and complete from 1 to 12 plus play +5 totaling 17.

  • @Alissonacioli Thanks for pointing, I made a small correction - test the new code.

  • I didn’t test if this last one of yours works, but I made a change to the line and left it like this: if (v.mes == m){ found = true; } and it worked partially, then I just had to sort with Sort() and do a function inside the Sort to sort the field month I gave it right. Thank you

3

You can create one by running from 1 to 12, and check if the iteration number already exists in your nameVendaArray, if not you populate with {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);

https://lodash.com/docs/4.16.4#merge

  • I got a piece of the code... In case I’m putting so in the array {mes:mes, venda:vendaMes} how do I get him to pick up and check the item month and if you add sale in a new array and if you do not have 0 ?

  • var defaultArrayMesVenda = [{mes: 1, sell: 0}, {mes:2, sell: 0}, {mes:3, sell: 0}, {mes:4, sell:0}, {mes:5, sell:0}, {mes:6, sell:0}, {mes:7, sell:0}, {mes:8, sell:0}, {mes:9, sell:0}, {mes:10, sell:0}, {mes:11, sell:0}, {mes:12, sell:0}]; &##Xa;_. unionBy(mesVendaArray, defaultArrayMesVenda, 'mes');

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);

Browser other questions tagged

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