Given a text array and an array of symbols, the current code performs the following:
It looks for symbols in the text array and if the next element is also a symbol, it combines both symbols (the current one and the next one) with a '/' between them into a new array. This creates pairs of symbols.
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'],
However, there are cases where there are 3 consecutive elements in the text array - 'XLM', 'DASH', 'COIN'. The output includes two pairs based on these consecutive symbols: 'XLM/DASH' and 'DASH/COIN'.
I would like to modify this behavior so that if there are no additional symbols after the third symbol, only the first and second symbols are returned as pairs.
Desired output from the text array:
['BCH/LTC','XLM/DASH']
If there is a fourth symbol present, then I want to return the third and fourth symbols in pairs as well.