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 of A1? The elements of A1 have to be part of r. – Andreas Commented Dec 27, 2019 at 18:33
  • There's nothing in mon between A1 and A2. 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
Add a ment  | 

4 Answers 4

Reset to default 4
  1. r is a string, therefore r.text is neither necessary nor correct.
  2. A1.indexOf(r) > -1 checks if the string r is included in the array A1. 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