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

134
Views
How to find all arrays and not just the first occurance using find method in java script?

Here is my code:

const string = `1
00:01:46,356 --> 00:01:49,893
test test test test

2
00:01:50,794 --> 00:01:54,998
test test test red test test

3
00:01:55,199 --> 00:01:58,267
test test red`;

const match = st.split('\n\n').find((e) => e.includes('red'));


console.log(match);

the problem is it returns only the first occurance (the word "red" in 2nd sentence) while there is another "red" in the last sentence. What should I do to get all occurrences?

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

0

Using find:

returns the value of the first element in the provided array that satisfies the provided testing function

Instead of find, you can use filter:

const match = string.split('\n\n').filter((e) => e.includes('red'));

const string = `1
00:01:46,356 --> 00:01:49,893
test test test test

2
00:01:50,794 --> 00:01:54,998
test test test red test test

3
00:01:55,199 --> 00:01:58,267
test test red`;

const match = string.split('\n\n').filter((e) => e.includes('red'));
console.log(match);

about 4 years ago · Juan Pablo Isaza Report

0

Just use filter instead of find.

const st = `1
00:01:46,356 --> 00:01:49,893
test test test test

2
00:01:50,794 --> 00:01:54,998
test test test red test test

3
00:01:55,199 --> 00:01:58,267
test test red`;

const match = st.split('\n\n').filter((e) => e.includes('red'));


console.log(match);

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!