Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
Check two arrays and its respective index as pair and find if a similar pair exist

I have a simple html code as below

<input type="text" id="key" name="key">
<input type="text" id="value" name="value">
<button id="check">Check</button>

and I have related jQuery code as well

var keyArray = [];
var valueArray = [];
$("#check").click(function() {
  var keyVal = $("#key").val();
  var valueVal = $("#value").val();
  keyArray.push(keyVal);
  valueArray.push(valueVal);
  console.log(keyArray);
  console.log(valueArray);
  for ($i = 0; $i < keyVal.length; $i++) {
    //Need to add some code here to check
  }
});

What I want is, whenever if someone click the Check button, it has to check if there is a similar item added before into the respective index of keyArray and valueArray. Eg: First I add 1 into the id key and 2 into the id value. If I add 1 and 2 into key and value fields a second time, it should prompt me such a pair already added.

How can I achieve this with JavaScript or jQuery?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

var keyArray = [];
var valueArray = [];
$("#check").click(function() {
  var keyVal = $("#key").val();
  var valueVal = $("#value").val();
  var exist=false;
  
  if(keyArray.length>0){
    for (i = 0; i < keyArray.length; i++) {
    if(keyArray[i]==keyVal && valueArray[i]==valueVal)
    {
      console.log("pair exist");
      exist=true;
      break;
    }
  }
  }  
  if(!exist)
  {
     keyArray.push(keyVal);
     valueArray.push(valueVal);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="key" name="key">
<input type="text" id="value" name="value">
<button id="check">Check</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want, you can introduce a third array and store data in it, and compare it with your value.

var keyArray = [];
var valueArray = [];
var newArray = [];
$("#check").click(function() {
  var keyVal = $("#key").val();
  var valueVal = $("#value").val();
  
  var isExist = false;
  for (i = 0; i < newArray.length; i++) {
     if(newArray[i].key == keyVal && newArray[i].value == valueVal ){
      isExist = true; 
      break;
     }
     else{
      isExist = false;
     }
  }
  
 
  if (isExist){
      alert("such a pair already added");
  }
  else{
     keyArray.push(keyVal);
     valueArray.push(valueVal);
     newArray.push({ key : keyVal, value : valueVal });
  }
  
  console.log(keyVal);
  console.log(valueVal);
  console.log(newArray);
  
  

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="key" name="key">
<input type="text" id="value" name="value">
<button id="check">Check</button>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda