admin管理员组文章数量:1314843
Consider an array A1 ["text", "test"]
and another array A2 ["onetest", "two", "threetext", "fourtext"]
I need to get the result as ["onetest", "threetext", "fourtext"]
This is what I currently have
A2.filter(r => r !== null && A1.indexOf(r) > -1);
This is returning empty list. How do I fix that?
Consider an array A1 ["text", "test"]
and another array A2 ["onetest", "two", "threetext", "fourtext"]
I need to get the result as ["onetest", "threetext", "fourtext"]
This is what I currently have
A2.filter(r => r !== null && A1.indexOf(r) > -1);
This is returning empty list. How do I fix that?
Share Improve this question edited Dec 27, 2019 at 18:36 user989988 asked Dec 27, 2019 at 18:30 user989988user989988 3,72612 gold badges66 silver badges157 bronze badges 5-
And why should (e.g.)
"onetest"
be part ofA1
? The elements ofA1
have to be part ofr
. – Andreas Commented Dec 27, 2019 at 18:33 -
There's nothing in mon between
A1
andA2
. Unless you're trying to filter by substring perhaps? – Heretic Monkey Commented Dec 27, 2019 at 18:35 - Yes I'm trying to filter by substring – user989988 Commented Dec 27, 2019 at 18:37
- You may want to read the answers to In javascript, how do you search an array for a substring match – Heretic Monkey Commented Dec 27, 2019 at 18:37
- Does this answer your question? Check if a string contains any element of an array in JavaScript – Heretic Monkey Commented Dec 27, 2019 at 18:53
4 Answers
Reset to default 4r
is a string, thereforer.text
is neither necessary nor correct.A1.indexOf(r) > -1
checks if the stringr
is included in the arrayA1
. This is not true for any of your items.
Instead of r.text
, just use r
. To determine if the string r
contains any of the substrings in A1
, you can use .some
and .includes()
:
const A1 = ["text", "test"];
const A2 = ["onetest", "two", "threetext", "fourtext"];
const result = A2.filter(r => r !== null && A1.some(a => r.includes(a)));
console.log(result);
I see two issues: first, the elements of A2
do not have a property called text
, so r.text
is undefined
and you probably just want to use r
instead.
Second, A1.indexOf
is not what you want. indexOf
is both an array method and a string method, and I think you're confusing them. The array method is used to find the index of an exact match, whereas the string method can be used to find the location of a substring. What you want is to determine whether any member of A1
is present as a substring in the given member of A2
. I suggest the Array.prototype.some
method, which determines whether a given predicate is true for any member of an array. Within this we can create a predicate using the string method indexOf
.
See if this works for you:
const A1 = ["text", "test"]
const A2 = ["onetest", "two", "threetext", "fourtext"]
// I need to get the result as ["onetest", "threetext", "fourtext"]
console.log(A2.filter(r => r !== null && A1.some(s => r.indexOf(s) > -1)))
You could filter by checking each value of the pattern string.
var a1 = ["text", "test"],
a2 = ["onetest", "two", "threetext", "fourtext"],
result = a2.filter(s => a1.some(p => s.includes(p)));
console.log(result);
A1.indexOf(r)
only checks if an element of the array matches exactly the given text, and is not the case, you're searching for a substring.
Try with this:
var a1 = ["text", "test"];
var a2 = ["onetest", "two", "threetext", "fourtext"];
var filtered = a2.filter( e => a1.some( e2 => e.indexOf( e2 ) != -1 ) );
console.log( filtered );
This will filter the second array checking for each element if the string contains some of the first array element.
本文标签: filter in typescriptjavascriptStack Overflow
版权声明:本文标题:filter in typescriptjavascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741971100a2407843.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论