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

83
Views
¿La cadena de verificación incluye una subcadena sin caracteres adicionales?

Estoy tratando de hacer que mi código ordene dos matrices y cree un diccionario que contenga valores de ambos, pero cuando los busco, termino obteniendo valores adicionales que no quiero. Por ejemplo, mientras busco entre los símbolos 'USD', puedo terminar con XXX-USDT cuando quiero específicamente USD, no USDT. ¿Cómo puedo asegurarme de que no haya caracteres adicionales antes o después?

 var symbols = ['ETH', 'BTC', 'USD', 'USDT']; var marketPairs = ['AAVE-USD', 'AAVE-USDT', 'AAVE-BTC', 'AAVE-ETH']; var marketDict = {}; for (let i = 0; i < symbols.length; i++) { marketDict[symbols[i]] = []; for (let j = 0; j < marketPairs.length; j++) { if (marketPairs[j].includes('-'+symbols[i]) || marketPairs[j].includes(symbols[i]+'-')) { marketDict[symbols[i]].push(marketPairs[j]); } } }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede usar termina con y comienza con aquí

 marketPairs[j].endsWith("-" + symbols[i]) || marketPairs[j].startsWith(symbols[i] + "-") 

 var symbols = ["ETH", "BTC", "USD", "USDT"]; var marketPairs = ["AAVE-USD", "AAVE-USDT", "AAVE-BTC", "AAVE-ETH"]; var marketDict = {}; for (let i = 0; i < symbols.length; i++) { marketDict[symbols[i]] = []; for (let j = 0; j < marketPairs.length; j++) { if ( marketPairs[j].endsWith("-" + symbols[i]) || marketPairs[j].startsWith(symbols[i] + "-") ) { marketDict[symbols[i]].push(marketPairs[j]); } } } console.log(marketDict);

about 4 years ago · Juan Pablo Isaza Report

0

Su condición if verifica si una cadena contiene -USD, no si coincide exactamente: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes

Si está buscando hacer coincidir el final/inicio de la cadena, puede usar .endsWith y beginWith:

 if (marketPairs[j].endsWith('-'+symbols[i]) || marketPairs[j].startsWith(symbols[i]+'-'))

( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith y https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference /Global_Objects/String/startsWith como referencia)

Alternativamente, puede usar RegEx en combinación con .search (si desea obtener una matriz de caracteres coincidentes) o .match (si solo desea un índice de la primera coincidencia):

 if (marketPairs[j].match(new RegExp(symbols[i] + '|' + symbols[j])) > -1)

( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp )

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!