I have come across various questions and answers related to this particular issue, but most of them involve using the includes
or indexOf
methods.
The problem at hand is how to filter an array (names
in this scenario) using two different arrays - one with the startsWith
criteria and the other with the endsWith
criteria.
var names = ['BOB','CATHY','JAKOB','AARON','JUSTICE','BARBARA','DANIEL','BOBBY','JUSTINE','CADEN','URI','JAYDEN','JULIE']
startPatterns = ['BO','JU']
endPatterns = ['EN','ICE']
//res = ['BOB','JUSTICE','JUSTINE','JULIE','JAYDEN','JUSTICE']
It is clear that using
names.filter(d => d.startsWith(startPatterns))
is not a feasible solution as startPatterns
is an array and not a string. The current approach I attempted is not only ineffective but also very slow:
res=[]
names.forEach(d => {
endPatterns.forEach(y => d.endsWith(y) ? res.push(d) : '')
startPatterns.forEach(s => d.startsWith(s) ? res.push(d) : '')})
console.log(res)