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

168
Views
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 answers
Answer question

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