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

174
Views
pequeño problema con la matriz en javascript, ¿cómo comparar la matriz con la cadena y encontrar el mismo elemento?

Tuve un problema con una matriz como esta. Primero, tengo una matriz y una cadena y debo comparar esta matriz y esta cadena. Si esta matriz tiene un elemento igual al elemento de la cadena, debo crear una nueva matriz que tenga este elemento. Por ejemplo :

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

chuoi = "PHP"

Cuando comparamos "chuoi" y "mang", como puede ver, tenemos "PHP", por lo que debo crear la matriz ['PHP']

aquí está mi código (mi idea)

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

Pero todavía no funciona, ¿podría darme algunas ideas para mí? Muchas gracias por tu tiempo.

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

0

forEach no devuelve ningún valor. En este caso puedes usar reducir

 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 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!