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

131
Visualizações
Log array element if string exists

I wanted to loop through an array and check if a string exists in the array elements and my code below partially works. The problem is currently it logs the array element if a specified string exists anywhere in the array element but what I want to do is log if the string is in the array element but also the same index position. To explain this better say one of my array elements is testing and the string I'm looking for is tes because tes is occurring in the index position 0,1,2 the element logs. But say my array element is not testing and the string I'm looking for is tes it won't log because even though the string exists it's in the wrong index. How can I achieve this? Thanks in advance.

const myArray = ['test blah', 'this is test', 'testing 234', 'nothing']

const check = 'te'
for (var i = 0; i < myArray.length; i++) {
  if (myArray[i].includes(check)) {
    //should print myArray[0] and myarray[2]
    console.log(myArray[i]);
  }
}

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

You can use startsWith()

const myArray = ['test blah', 'this is test', 'testing 234', 'nothing']

const check = 'tes'
for (let i = 0; i < myArray.length; i++) {
  if (myArray[i].startsWith(check)) {
    console.log(myArray[i]);
  }
}

about 4 years ago · Santiago Gelvez 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