admin管理员组文章数量:1323323
Browsed several different questions/answers surrounding this question, but most rely on includes
or indexOf
Problem: How any array to filter (names
in this case). I need to filter it using 2 different arrays, one of which has startsWith
criteria and the other has 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']
Obviously you cannot do names.filter(d => d.startsWith(startPatterns))
because startPatterns
is not a string but a array. Something like this isn't working and is terrible slow too:
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)
Browsed several different questions/answers surrounding this question, but most rely on includes
or indexOf
Problem: How any array to filter (names
in this case). I need to filter it using 2 different arrays, one of which has startsWith
criteria and the other has 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']
Obviously you cannot do names.filter(d => d.startsWith(startPatterns))
because startPatterns
is not a string but a array. Something like this isn't working and is terrible slow too:
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)
Share
asked Dec 13, 2019 at 20:06
K.J.J.KK.J.J.K
4396 silver badges14 bronze badges
2
-
Your code works, it just has problems with duplicates. An easier way is to
names.filter(name => startPatterns.some(p => name.startsWith(p)) || endPatterns.some(p => name.endsWith(p)));
– ASDFGerte Commented Dec 13, 2019 at 20:11 -
2
forEach
does not return anything, so the usage of an expression bodied function and there a misappropriation of?:
topush
– crashmstr Commented Dec 13, 2019 at 20:11
2 Answers
Reset to default 7You can use Array.prototype.some
on the pattern arrays to achieve this:
let filtered = names.filter(name => (
startPatterns.some(pattern => name.startsWith(pattern)) ||
endPatterns.some(pattern => name.endsWith(pattern))
))
The logic here being "Return true
if the name begins with at least one of the start patterns OR ends with at least one of the end patterns".
You could build a regular expression and check the string against the pattern.
var names = ['BOB','CATHY','JAKOB','AARON','JUSTICE','BARBARA','DANIEL','BOBBY','JUSTINE','CADEN','URI','JAYDEN','JULIE'],
startPatterns = ['BO','JU'],
endPatterns = ['EN','ICE'],
regexp = new RegExp(`^(${startPatterns.join('|')})|(${endPatterns.join('|')})$`),
result = names.filter(s => regexp.test(s));
console.log(result);
A non regular expression approach with an array with the methods and wanted values.
var names = ['BOB','CATHY','JAKOB','AARON','JUSTICE','BARBARA','DANIEL','BOBBY','JUSTINE','CADEN','URI','JAYDEN','JULIE'],
patterns = [['startsWith', ['BO','JU']], ['endsWith', ['EN','ICE']]],
result = names.filter(s => patterns.some(([k, values]) => values.some(v => s[k](v))));
console.log(result);
本文标签: javascriptFilter array with multiple startsWith and endsWith argumentsStack Overflow
版权声明:本文标题:javascript - Filter array with multiple startsWith and endsWith arguments - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742141001a2422578.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论