Two arrays in foreach js

Asked

Viewed 655 times

0

I have a Javascript function that sends requests with the content of a line textarea, is a looping that traverses an array and sends requests with the data of these indexes of the array, the problem is that we need to create 1 more array with the content of another textarea and send simultaneously with the array of the first textarea, in the case:

<script>
  function enviar(){
    var bin = $("#bin_id").val();
    var linhaenviar = bin.split("\n");
    var cor = coloracao.split("\n"); //esse e o textarea do qual quero criar o array
    var index = 0;
    linhaenviar.forEach(function(value){
      setTimeout(
        function(){
          $.ajax({
            url: 'bancodedados.php',
            type: 'POST',
            dataType: 'json',
            data: 'nome=' + value + 'cor=' +arraycor,
            success: resultado
        })
      }, 10 * index);
    index = index + 0;
    })
  }
</script>
  • what I’m doing this is the best way

  • I am sending line by line from a single textarea, but need to send line by line from two textareas at the same time in a single request

  • no, the quantity will vary depending on the data received

  • So it pays to make a request only, and work with the data on back-end.

  • this will open 1 process only using a looping in php itself and let the return of the answers slow, need the looping stay in js this way opened multiple processes in php file working as threads and the return of each request will be faster

  • Multiplying the time in 10 * index serves what?

Show 1 more comment

1 answer

2

I believe this is what you want. Before 2 arrays, make a forEach in the largest and pull each index simultaneously from the smallest to the limit:

function enviar(){
    var bin = $("#bin_id").val();
    var coloracao = $("#coloracao").val();
    var linhaenviar = bin.split("\n");
    var cor = coloracao.split("\n"); //esse e o textarea do qual quero criar o array
    var index = 0;
    
    var comp = linhaenviar.length >= cor.length;
    var maior = comp ? linhaenviar : cor;

    maior.forEach(function(value, i){

      var arraycor = comp ? (cor[i] != null ? cor[i] : ''): value;
      var linha = !comp ? (linhaenviar[i] != null ? linhaenviar[i] : ''): value;

      setTimeout(
         function(){
           console.log(linha+" / "+arraycor);
//            $.ajax({
//               url: 'bancodedados.php',
//               type: 'POST',
//               dataType: 'json',
//               data: 'nome=' + linha + 'cor=' +arraycor,
//               success: resultado
//        })
      }, 10 * index);
    index = index + 0;
    })
  }
textarea{
height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="bin_id">linha1
linha2
linha3
linha4
linha5
linha6</textarea>

<textarea id="coloracao">cor1
cor2
cor3
cor4
cor5</textarea>

<br />
<button onclick="enviar()">Enviar</button>

Browser other questions tagged

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