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

125
Views
Emparejamiento de símbolos en javascript

Tengo un texto y una matriz de símbolos , por lo que el código actual hace esto:

Encuentre un símbolo de la matriz de texto y si el siguiente elemento también es un símbolo, empuje ambos (el símbolo actual y el siguiente elemento, que también es un símbolo) con uno / entre ellos a una nueva matriz. (Haga un par de símbolos)

 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','COIN','ETH','LTC','BNB','BCH','XRP']; //Return all the pair in text 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; }, []); //output : //['BCH/LTC','XLM/DASH','DASH/COIN'],

Pero aquí, hay 3 elementos consecutivos en la matriz de texto, 'XLM', 'DASH' ,'COIN' , y como puede ver en la salida, devuelve dos pares de 3 símbolos consecutivos: 'XLM/DASH','DASH/COIN'

Quiero ignorarlo y si no hay otro símbolo después del tercer símbolo, simplemente devuelva el primer y el segundo símbolo en pares.

lo que quiero de la matriz de texto: ['BCH/LTC','XLM/DASH']

Y si hay un cuarto símbolo, devolver el tercer y cuarto símbolo en pares

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

0

Intenta usar el bucle for

 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', 'COIN', 'ETH', 'LTC', 'BNB', 'BCH', 'XRP']; //Return all the pair in text const set = new Set(symbols); let result = [] for (let i = 0; i < text.length; i += 2) { let curr = text[i], next = text[i + 1]; if (set.has(curr) && set.has(next)) { result.push(`${curr}/${next}`) } } console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

Otro enfoque es uno adaptado del trabajo que ha realizado con reduce . La idea es agregar una variable, que rastreará si el último segundo par coincide con el primer par de la siguiente coincidencia inmediata, si existe.

 const text = ['XLM', 'BTC','08', '324','ETH', '233','yyyy', '30000','XRP', 'xxxxxGG','llll', '546','BCH', 'LTC','xxxyyy', '435','XLM', 'DASH','COIN', 'ETH']; const symbols = ['XLM', 'XTZ', 'BTC', 'DASH', 'COIN', 'ETH', 'LTC', 'BNB', 'BCH', 'XRP']; let lastSecondPairI = -1; // must not be a index in first iteration 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)){ // check if it does not match the current element (by i) if(lastSecondPairI !== i){ acc.push(`${curr}/${next}`); // update the value every time there's not a match lastSecondPairI = i+1; } } 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!