1

I want to find multiple values in an array. When searching, I want to be able to use something like a LIKE statement from SQL.

arr = ['end', 'start_date', 'hello', 'end_dt', 'pub_date']

When I do:

let el = arr.find(a => a.includes('date') || a.includes('dt'));

Right now it only returns the first value it finds, start_date

I need it to return:

start_date
end_dt
pub_date

How do I do that?

1
  • 4
    Use filter instead of find Commented Apr 16, 2020 at 20:32

1 Answer 1

3

Array.prototype.find returns only the first match for the return conditions.

Use Array.prototype.filter:

let arr = ['end', 'start_date', 'hello', 'end_dt', 'pub_date']
console.log(arr.filter(a => a.includes('date') || a.includes('dt')))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.