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

188
Views
encuentre un símbolo de la matriz de texto y si el siguiente elemento también es un símbolo, presione ambos

Tengo una matriz de texto y los símbolos y el código actual hacen esto: busque en la matriz de texto y si hay un elemento de la matriz de símbolos en la matriz de texto , introdúzcalo en una nueva matriz (coinsArr)

Quiero encontrar un símbolo de la matriz de texto y si el siguiente elemento también es un símbolo, presione ambos (el símbolo actual y el elemento y el siguiente elemento, que también es un símbolo) con uno / entre ellos.

Así que este es mi código:

 const text = [ 'aaaa', 'BTC', '08', '324', 'ETH', '233', 'yyyy', '30000', 'XRP', 'xxxxxGG', 'llll', '546', 'BCH', 'LTC', 'xxxyyy', '435', 'XLM', 'DASH', 'COIN' ]; const symbols = ['XLM', 'XTZ', 'BTC', 'DASH', 'ETH', 'LTC', 'BNB', 'BCH', 'XRP']; const coinsArr = []; for (let i = 0; i < text.length; i++) { for (let j = 0; j < symbols.length; j++) { const el = symbols[j]; if (el == text[i]) { coinsArr.push(el); } } } console.log({ 'coinsArr': coinsArr });

Y da esta salida (solo busca en la matriz de texto y encuentra símbolos):

 ['BTC','ETH','XRP','BCH','LTC','XLM','DASH','COIN']

Pero quiero esta salida:

 ['BCH/LTC','XLM/DASH']
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede lograr fácilmente el resultado usando reducir y optimizar el acceso de symbols usando Set

 const text = [ 'aaaa', 'BTC', '08', '324', 'ETH', '233', 'yyyy', '30000', 'XRP', 'xxxxxGG', 'llll', '546', 'BCH', 'LTC', 'xxxyyy', '435', 'XLM', 'DASH', 'COIN' ]; const symbols = ['XLM', 'XTZ', 'BTC', 'DASH', 'ETH', 'LTC', 'BNB', 'BCH', 'XRP']; const set = new Set(symbols); const result = text.reduce((acc, curr, i, src) => { const next = src[i + 1]; if (set.has(curr) && set.has(next)) acc.push(`${curr}/${next}`); return acc; }, []); console.log(result);

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!