Javascript: Prioritize value in concatenation (.Concat)

Asked

Viewed 70 times

0

I’m studying about the Concat and priorities
The following code currently concatenates the Texts values in the field ValFin
However I would like to get the priority of Field 4 (Prival04 value 1) was less than field 2 (Prival02 with the value 2), field 4 sort first

function teste(){
  var a = document.getElementById('val01').value;
  var b = document.getElementById('val02').value;
  var c = document.getElementById('val03').value;
  var d = document.getElementById('val04').value;
  var f = a.concat(b).concat(c).concat(d);
  document.getElementById("ValFin").value = f;
  console.log(document.getElementById("ValFin").value);
}
<html>
  <head>
  </head>
  <body>
    <label>Valor 01</label>
    <input id="val01" type="text" /> 
    <label>Prioridade do Valor 01</label>
    <input id="Prival01" type="text" maxlength="1" /> <br>    
    <label>Valor 02</label>
    <input id="val02" type="text" />
    <label>Prioridade do Valor 02</label>
    <input id="Prival02" type="text" maxlength="1" /> <br>
    <label>Valor 03</label>
    <input id="val03" type="text" />
    <label>Prioridade do Valor 03</label>
    <input id="Prival03" type="text" maxlength="1" /> <br>
    <label>Valor 04</label>
    <input id="val04" type="text" />    
    <label>Prioridade do Valor 04</label>
    <input id="Prival04" type="text" maxlength="1" /> <br>
    <br>
    <button id="Calc" onclick="teste();">FuncaoMontar</button>
    <input id="ValFin" type="text" />
  </body>
</html>

  • You have to put the priorities in an array, sort and then have a cycle that passes in that array and fetches the value of that priority to concatenate.

1 answer

2


From what I understand you want to order according to the camps of Prival right? I made an example by adding the values in an array, where you can manipulate in several ways...

Follow the example:

function teste(){
  var array = [];
  var a = {
    value: document.getElementById('val01').value,
    order: document.getElementById('Prival01').value
  };
  var b = {
    value: document.getElementById('val02').value,
    order: document.getElementById('Prival02').value
  };
  
  var c = {
    value: document.getElementById('val03').value,
    order: document.getElementById('Prival03').value
  };
  var d = {
    value: document.getElementById('val04').value,
    order: document.getElementById('Prival04').value
  };
  
// Adcionei os objetos no array
array.push(a, b, c, d);

// Utilizar o método sort para ordernar conforme o campo de ordenação
array.sort((a, b) => a.order < b.order ?  -1 : 1);
 
// Transoforma o array de objetos em um array de string e após faz o join para uma string sem espaços
var f = array.map(({value}) => value).join('');
  
document.getElementById("ValFin").value = f;
console.log(document.getElementById("ValFin").value);
}
    <label>Valor 01</label>
    <input id="val01" type="text" /> 
    <label>Prioridade do Valor 01</label>
    <input id="Prival01" type="text" maxlength="1" /> <br>    
    <label>Valor 02</label>
    <input id="val02" type="text" />
    <label>Prioridade do Valor 02</label>
    <input id="Prival02" type="text" maxlength="1" /> <br>
    <label>Valor 03</label>
    <input id="val03" type="text" />
    <label>Prioridade do Valor 03</label>
    <input id="Prival03" type="text" maxlength="1" /> <br>
    <label>Valor 04</label>
    <input id="val04" type="text" />    
    <label>Prioridade do Valor 04</label>
    <input id="Prival04" type="text" maxlength="1" /> <br>
    <br>
    <button id="Calc" onclick="teste();">FuncaoMontar</button>
    <input id="ValFin" type="text" />

  • Exactly !! Thank you so much for helping @André !! I will study the code here

Browser other questions tagged

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