Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
Check string includes a substring without extra characters?

I am trying to get my code to sort through two arrays and create a dictionary that contains values from both, but when I search through them I end up getting extra values that I don't want. For example while searching through 'USD' symbols, I can end up with XXX-USDT when I specifically want USD, not USDT. How can I make sure there aren't extra characters before or after?

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 Respostas
Responde à pergunta

0

You can use endsWith and startsWith here

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 Relatório

0

Your if condition checks if a string contains -USD, not if it exactly matches it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes

If you're looking to match the end/start of the string, you can use .endsWith and startsWith:

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 and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith for reference)

Alternatively, you can use RegEx in combination with .search (if you want to get an array of matched chars) or .match (if you just want an index of the first match):

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda