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

169
Visualizações
little problem with array in javascript, how to compare array with string and find the same element?

I had a problem with array like this. First, I have an array and a string and I must compare this array and this string. If this array has an element the same as the element of the string, I must create a new array which has this element. For example :

mang : ['Javascript', 'PHP', 'H PHP']

chuoi = "PHP"

When we compared "chuoi" and "mang", as you can see, we have "PHP", so I must create the array ['PHP']

here is my code (my idea)

function findStringsInArrayByKeyword(keyword, strings) {
var chuoi = "PHP";
var mang = ['Javascript', 'PHP', 'Học PHP'];
mang.forEach(function(a)){
if (a===chuoi) {
     return [a];
}
}
}

But it still does not run, could you please give me some ideas for me? Thank you very much for your time.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

forEach does not return any value. In this case you can use reduce

function findStringsInArrayByKeyword(keyword, strings) {
  var chuoi = "PHP";
  var mang = ['Javascript', 'PHP', 'Học PHP'];
  return mang.reduce((acc, curr) => {
    if (curr === chuoi) {
      acc.push(curr);
    }
    return acc;
  }, [])
}

console.log(findStringsInArrayByKeyword())

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