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

116
Views
Pairing of symbols in javascript

I have a text and a symbols array , so current code does this :

Find a symbol from the text array and if next element is also a symbol, push both (the current symbol and the next element, which is also a symbol)with one / between them to a new array.(Make a symbol pair)

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'],

But Here , there are 3 consecutive elements in text array , 'XLM', 'DASH' ,'COIN', and as you see in the output ,it returns two pair of 3 consecutive symbols :'XLM/DASH','DASH/COIN'

I want to ignore it and if there is no other symbol after the third symbol, just return the first and second symbols in pairs

what i want from the text array : ['BCH/LTC','XLM/DASH']

And if there is a fourth symbol, return the third and fourth symbols in pairs

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

0

Try using for loop

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

Another approach is one adapted from the work you have done with reduce. The idea is to add a variable, which will track whether the last second pair matches the first pair of the immediate next match if it exists.

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!