Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
Verifique dos matrices y su índice respectivo como par y encuentre si existe un par similar

Tengo un código html simple como el siguiente

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

y también tengo código jQuery relacionado

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

Lo que quiero es que, cada vez que alguien haga clic en el botón Check , debe verificar si se agregó un elemento similar antes en el índice respectivo de keyArray y valueArray . Por ejemplo: primero agrego 1 a la key de id y 2 al value de id . Si agrego 1 y 2 en los campos de key y value por segunda vez, debería indicarme such a pair already added .

¿Cómo puedo lograr esto con JavaScript o jQuery ?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

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 Report

0

Si lo desea, puede introducir una tercera matriz y almacenar datos en ella y compararla con su valor.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!