Get specific result by comparing array’s through Javascript’s for

Asked

Viewed 72 times

2

I need help printing a result through the code below:
Expected result: e5f6abb0c4b74c20a92195375bef2434 | 1 | 2020-04-01 - c7aead0f41f90cf4eace601998bd78c4 | 1 | 2020-04-02
(error) Result obtained: e5f6abb0c4b74c20a92195375bef2434 | 1 | 2020-04-01 - c7aead0f41f90cf4eace601998bd78c4 | 2 | 2020-04-01,2020-04-02

$array_cods = "e5f6abb0c4b74c20a92195375bef2434|c7aead0f41f90cf4eace601998bd78c4";
$array_dias = "2020-01-15,2020-01-20,2020-01-22,2020-01-27,2020-01-29,2020-02-03,2020-02-05,2020-02-10,2020-02-12,2020-02-17,2020-02-19,2020-02-24,2020-02-26,2020-03-02,2020-03-04,2020-03-09,2020-03-11,2020-03-16,2020-03-18,2020-03-23,2020-03-25,2020-03-30,2020-04-01,2020-04-06,2020-04-08,2020-04-13,2020-04-15,2020-04-20|2020-04-02,2020-04-07,2020-04-08,2020-04-09,2020-04-14,2020-04-15,2020-04-16,2020-04-21";
$array_datas = "2020-04-01,2020-04-02"
var allcods = "<?php echo $array_cods; ?>";
var cods = allcods.split('|');

var alldias = "<?php echo $array_dias; ?>";
var diasa = alldias.split('|');
var diasb = String(alldias.split('|'));

var alldatas = "<?php echo $array_datas; ?>";
var datas = alldatas.split(',');

for(var i=0; i<cods.length; i++) { //--- Sempre será executado pela quantidade de cods
    executar();
}

function executar() {
    for(var i=0; i<cods.length; i++) {
        var dias = diasb.split(',');

        var arr = dias.some(dias => datas.includes(dias));
        var b = String(dias);
        var c = String(datas);
        var a = b.indexOf(parseInt(c));

        if (arr == true) {
            console.log(cods+" | "+datas.length+" | "+c);
            break;
        } else { 
            console.log(cods+" | Não");
            break;
        }
    }
}

The Cód. c7aead0f41f90cf4eace601998bd78c4 cannot get 2, because it does not have the date 2020-04-01

See an example of the error using the dates 2020-04-01 to 2020-04-13


inserir a descrição da imagem aqui


In Cód. c7aead0f41f90cf4eace601998bd78c4 should be 4


Sorry, the code is full of tests and tests Example in Jsfiddle

The code below is very scrambled and full of tests, but it’s working:

function calcDayAtes() { //----- Somar dias do Atestado
  //var idnClient = document.getElementById("idClient").value;
  var dtInicat = document.getElementById('dtinicat').value;
  var dtTerat = document.getElementById('dtterat').value;

  var resultados = "2020-01-15,2020-01-20,2020-01-22,2020-01-27,2020-01-29,2020-02-03,2020-02-05,2020-02-10,2020-02-12,2020-02-17,2020-02-19,2020-02-24,2020-02-26,2020-03-02,2020-03-04,2020-03-09,2020-03-11,2020-03-16,2020-03-18,2020-03-23,2020-03-25,2020-03-30,2020-04-01,2020-04-06,2020-04-08,2020-04-13,2020-04-15,2020-04-20|2020-04-02,2020-04-07,2020-04-08,2020-04-09,2020-04-14,2020-04-15,2020-04-16,2020-04-21";
  var results = resultados.split('|');

  var nwdtInicat = dtInicat.split('/').reverse().join('-');
  var nwdtTerat = dtTerat.split('/').reverse().join('-');

  var pinic = nwdtInicat.split("-");
  var iano = pinic[0];
  var imes = pinic[1] - 1;
  var idia = pinic[2];
  var newInicial = new Date(iano, imes, idia);

  var pterm = nwdtTerat.split("-");
  var tano = pterm[0];
  var tmes = pterm[1] - 1;
  var tdia = pterm[2];
  var newFinal = new Date(tano, tmes, tdia);

  var milissegundos_por_dia = 1000 * 60 * 60 * 24;
  var data_inicial = new Date(newInicial.getTime() - 0 * milissegundos_por_dia);
  var data_final = new Date(newFinal.getTime() + 1 * milissegundos_por_dia);

  //alert(data_inicial);

  if ((dtInicat) && (dtTerat)) {

    //console.log(data_inicial);
    //console.log(data_final);

    //if (nwdtInicat > nwdtTerat) { msgErro("default|Conflito nas datas! <br> A data inicial não pode ser maior que a data final!|1"); return false; }
    //$('.box-ates-ben').load("data/boss/servAbnAtesCab.php?idben="+idnClient+"&atestado=novo&dta="+nwdtInicat+"&dtb="+nwdtTerat);

    //----- Início: Calcula datas da Semana
    var dateStart = data_inicial; //new Date(nwdtInicat+" 00:00:00");
    var dateEnd = data_final; //new Date(nwdtTerat+" 00:00:00");
    var dateRange = rangeDate(dateStart, dateEnd);
    var weeks = "1,3|2,3,4";
    var week = weeks.split('|');

    var codstur = "e5f6abb0c4b74c20a92195375bef2434|c7aead0f41f90cf4eace601998bd78c4";
    var codtur = codstur.split('|');


    //alert(weeks);

    for (var i = 0; i < week.length; i++) {
      var wekg = String(week[i].split(','));
      var codturg = String(codtur[i].split(','));
      var dias = String(results[i].split(','));
      const dateFiltered = filterDateByDays(dateRange, wekg);

      //const dateFiltered = filterDateByDays(dateRange, wekg);

      valDates = dateFiltered.map(value => value.toLocaleDateString("pt-br").split('/').reverse().join('-'));

      var arrayall = String(codturg + '_' + valDates + '|' + dias);
      //var arraydta = Array(codw[0]+'_'+valDates);
      //var arraydtb = arraydta.split(',');

      //alert(valDates);
      execVal(arrayall);
      //console.log(arrayall);
      //break;
    }

    function filterDateByDays(range, days) {
      if (!Array.isArray(days)) {
        days = wekg;
      }
      return range.filter(value => days.includes(value.getDay()));
    }

    function rangeDate(dateStart, dateEnd) {
      var dates = [];

      while (dateStart < dateEnd) {
        dates.push(new Date(dateStart.getTime()));
        dateStart.setDate(dateStart.getDate() + 1);
      }
      return dates;
    }
    //----- Final: Calcula datas da Semana
/*
    //----- Início: Pegar CODTUR
    var valores = "<?php echo $array_results; ?>";
    var resCod = valores.split('|');
    var cods = String(resCod);
    var codt = cods.split('_');

    for (var i = 0; i < codt.length; i++) {
      var codg = codt[i].split('_');
      var codh = String(codg);
      var codi = codh.substring(codh.lastIndexOf(',') + 1);
      var caracteres = codi.length;
      //if (caracteres > 10) { console.log(codi); }
    }
    //----- Início: Pegar CODTUR
*/

    function execVal(arrayall) {
      var diasa = arrayall.split('|');
      var diasb = diasa[1];
      var dias = diasb.split(',');

      var diasx = String(diasb.split(','));


      var cods = arrayall.split('_');
      var codtur = cods[0];

      var codta = diasa[0];
      var codtb = codta.split('_');
      var codtc = codtb[1];
      var datasa = String(codtc.split(','));
      var datas = codtc.split(',');

      //var datasb = String(codtc.split(','));
      //var datasx = datasb.split(',');

      //var o = datas.map(Object);
      //var o = Object.parse(datas);
      //typeof(editableObj.datas);
      //var nwdatas = new Object(datas);
      //console.log(datas);

      for (var i = 0; i < cods.length; i++) {
        //var dias = results[i].split(',');
        //var codturg = String(codtur[i].split(','));
        //var caracteres = datas.length;
        //var arr = dias.some(dias => datas.includes(dias));
        var arr  = dias.filter(dias => datas.includes(dias))

        var b = String(dias);
        var c = String(datas);
        var a = b.indexOf(parseInt(c));

        //var arr = datas.indexOf(angular(results[i]);
        //if (datas.includes(datas) in results[i]) {
        //var verif = (dias.indexOf(datas) != -1);
        //if (verif === true) {
        //if (datas.length in dias === true) {

        //if (arr == true) {
        if (arr.length > 0) {
          //console.log(codtur + " | " + datas.length + " | " + c);
          console.log(codtur+" | "+arr.length+" | "+String(arr));
          //Alert(codtur+" | "+datas.length+" | "+c);
          //alert(datas.length);
          //if (caracteres > 10) { }
          break;
        } else {
          //if (arr == false) {
          console.log(codtur + " | Não");
          //Alert(codtur+" | Não");
          //console.log('Não');
          //alert(datas);
          break;
        }

      }
    }


  }
}
<?php
$all_weeks = "1,3|2,3,4";
$array_codtur = "e5f6abb0c4b74c20a92195375bef2434|c7aead0f41f90cf4eace601998bd78c4";
$array_results_dias = "2020-01-15,2020-01-20,2020-01-22,2020-01-27,2020-01-29,2020-02-03,2020-02-05,2020-02-10,2020-02-12,2020-02-17,2020-02-19,2020-02-24,2020-02-26,2020-03-02,2020-03-04,2020-03-09,2020-03-11,2020-03-16,2020-03-18,2020-03-23,2020-03-25,2020-03-30,2020-04-01,2020-04-06,2020-04-08,2020-04-13,2020-04-15,2020-04-20|2020-04-02,2020-04-07,2020-04-08,2020-04-09,2020-04-14,2020-04-15,2020-04-16,2020-04-21";
?>

  Entre com uma data inicial e final:
  <br><br>
  <label>Data inicial
<input type='text' name='dtinicat' value="01/04/2020" id='dtinicat'placeholder='00/00/0000' maxlength='16' style='width:120px;'>
</label>
  <label>Data final
<input type='text' name='dtterat' value="02/04/2020" id='dtterat'placeholder='00/00/0000' maxlength='16' style='width:120px;'>
</label>
  <br><br>
  <input type='button' value='Executar' onclick='calcDayAtes();' style='width:80px;height:30px'>
  <br><br> OBS: As datas vão ser comparadas com os dias da semana. "1,3|2,3,4"

  • If you do not mount an example working with all the code there is no way to help. For example, in the piece of code you have placed the variable arr, where it comes from? Can’t enternder like this.. has already debugged and tried to figure out what’s wrong?

  • Corrected @Ricardopunctual - Complete and messy code, because I’m still stirring a lot and didn’t have time to clean: [https://jsfiddle.net/brendowsilas/a3vq79mb/30/]

  • In the "Show code snippet" is working... put the date there "procês" see

1 answer

3


You could do something like this:

var allcods = 
"e5f6abb0c4b74c20a92195375bef2434|c7aead0f41f90cf4eace601998bd78c4";
var cods = allcods.split('|');

var alldias = "2020-01-15,2020-01-20,2020-01-22,2020-01-27,2020-01-29,2020-02- 
03,2020-02-05,2020-02-10,2020-02-12,2020-02-17,2020-02-19,2020-02-24,2020-02- 
26,2020-03-02,2020-03-04,2020-03-09,2020-03-11,2020-03-16,2020-03-18,2020-03- 
23,2020-03-25,2020-03-30,2020-04-01,2020-04-06,2020-04-08,2020-04-13,2020-04- 
15,2020-04-20|2020-04-02,2020-04-07,2020-04-08,2020-04-09,2020-04-14,2020-04- 
15,2020-04-16,2020-04-21";
var dias = alldias.split('|');

var alldatas =  "2020-04-01,2020-04-02"
var datas = alldatas.split(',');

executar();

function executar() {
    for(var i=0;i< cods.length;i++){
        var diasCod = dias[i].split(',')
        var cod = cods[i]

        var filtro  = diasCod.filter(dias => datas.includes(dias))

        if (filtro.length > 0) {
            console.log(cod+" | "+filtro.length+" | "+String(filtro));
        } else { 
           console.log(cod+" | Não");
        }
    }    
}

As a response to the implementation of this programme:

e5f6abb0c4b74c20a92195375bef2434 | 1 | 2020-04-01
c7aead0f41f90cf4eace601998bd78c4 | 1 | 2020-04-02

This program takes the days of the respective code:

var diasCod = dias[i].split(',')

because days[0] have every day the first code "e5f6abb0c4b74c20a92195375bef2434" and the same with the other code

then take the code in question:

var cod = cods[i]

Then filter the days of that code by taking every day within "diasCod" that exist in the list "dates":

var filter = diasCod.filter(dias => datas.includes(dias))

If you have someone in the "filter" list print on the screen who the elements of this list and which code are:

if (filtro.length > 0) {
    console.log(cod+" | "+filtro.length+" | "+String(filtro));
}

If you do not print that the code does not have those dates:

else { 
    console.log(cod+" | Não");
}

If you had any questions, I could check the commands:

Split

Filter

Because from the program you posted it seems to me that you have a certain doubt about how the split works.

I hope I help you !

  • Killed to cock friend @Lucasramos... was it here: var filtro = diasCod.filter(dias => datas.includes(dias)) if (filtro.length > 0) May God bless you... thank you so much... thanks so much man... saved me saw!

  • Amigo @Lucasramos, I put these two parts there in "Show code piece" for you to see... Hug! God enlighten you

  • I’m glad I helped :) all the best !!

Browser other questions tagged

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