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

90
Views
Cómo comparar el elemento de la lista correspondiente con JavaScript

Tengo una lista como esta:

 [EMP004: BPCE-RNHC-25G8, EMP003: 8FIW-9JRB-NY4J, EMP005: 7QF2-6HI9-XKZZ, EMP002: SG8P-YQKG-ZV3C, EMP001: PBF7-WZHT-WPZR]

Lo que trato de lograr: el usuario ingresa la primera clave y luego el sistema escaneará la lista y les mostrará la clave correspondiente. Por ejemplo, si el usuario ingresa "EMP004", debería alertar BPCE-RNHC-25G8 por ellos.

Idea: ¿Cómo puedo separar la primera clave y la segunda clave con : Aparte de eso, ¿cómo puedo saber encontrar el resultado correspondiente? ¿Alguien sabe cómo puedo resolver esto solo con JavaScript? Gracias por adelantado.

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

0

La lista de muestra que ha proporcionado es sintácticamente incorrecta. Puede crear un objeto de matriz. Luego puede crear una función que tome la clave como parámetro de la siguiente manera:

 const list = [ { EMP004: 'BPCE-RNHC-25G8' }, { EMP003: '8FIW-9JRB-NY4J' }, { EMP005: '7QF2-6HI9-XKZZ' }, { EMP002: 'SG8P-YQKG-ZV3C' }, { EMP001: 'PBF7-WZHT-WPZR' } ]; const getValByKey = (key) => Object.values(list.find(o => Object.keys(o)[0] === key ))[0]; console.log(getValByKey('EMP004')); console.log(getValByKey('EMP002'));

Actualización: para elementos de cadena, puede probar de la siguiente manera:

 const list = ['EMP004: BPCE-RNHC-25G8', 'EMP003: 8FIW-9JRB-NY4J', 'EMP005: 7QF2-6HI9-XKZZ', 'EMP002: SG8P-YQKG-ZV3C', 'EMP001: PBF7-WZHT-WPZR']; const getValByKey = (key) => list.find(item => item.split(':')[0].trim() === key ).split(':')[1].trim(); console.log(getValByKey('EMP004')); console.log(getValByKey('EMP002'));

about 4 years ago · Juan Pablo Isaza Report

0

You can do like this: function search(arr, sub) { sub = sub.toLowerCase(); matchedIndex = arr.map((str, index) => { if ( str.toLowerCase().startsWith(sub.slice(0, Math.max(str.length - 1, 1))) ) { return index; } }); if(matchedIndex.length>0){ console.log(matchedIndex); matchedIndex = matchedIndex.filter(function( element ) { return element !== undefined; }); return matchedIndex[0] } } var arr = ["EMP004: BPCE-RNHC-25G8", "EMP003: 8FIW-9JRB-NY4J", "EMP005: 7QF2-6HI9-XKZZ", "EMP002: SG8P-YQKG-ZV3C", "EMP001: PBF7-WZHT-WPZR"]; var index = search(arr, 'EMP004'); if(index>-1){ matchedvalue=arr[index].split(':'); console.log(matchedvalue[1]); } else{ console.log("Not found") }
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!