0

I'm messing around with the the example code on the MDN page detailing Array.prototype.filter(), and I'm finding something interesting happening.

I modified the example code to be the following:

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.includes('e', 'i'));

console.log(result);
// expected output: const result = words.filter(word => word.includes('e', 'i'));

console.log(result); // expected output: Array ["elite", "exuberant", "destruction", "present"]

And it works, however when I modify it so those two characters are located in a variable (either declared with const OR var)...

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
var test = ['e', 'i'];
const result = words.filter(word => word.includes(test));

console.log(result);
// expected output: Array ["elite", "exuberant", "destruction", "present"]
// ACTUAL OUTPUT: Array []

Lastly, if I change test to test[0,1] I get the first result - as expected.

Other similar looking questions didn't seem to help me, and I'm not quite sure what's happening here, or how I can make this work.

3
  • 1
    includes('e', 'i') is not the same as includes(['e', 'i']) Commented Jul 9, 2021 at 14:09
  • 1
    replace .inculdes(test) with .includes(...test). Commented Jul 9, 2021 at 14:19
  • 1
    The real issue is that you never match with i here, so the version without a variable is also incorrect. Commented Jul 9, 2021 at 14:52

1 Answer 1

1

The problem is that the version with a variable does not do the same thing as the version that has no variable.

word.includes('e', 'i') is not the same as word.includes(['e', 'i']) (although neither do what you think, as noted below).

If you want store your arguments in an array and have them applied as multiple arguments (spread out) in your function call , instead of as a single array argument, you can use the spread syntax:

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
var test = ['e', 'i'];
const result = words.filter(word => word.includes(...test));

console.log(result);

The real issue

The real issue here is that you are not calling the function correctly. Check the MDN Docs for String#includes:

includes(searchString)

searchString A string to be searched for within str.

This function only takes a single parameter. So when you word.includes('e', 'i'), you should not expect it to match on words that has "i" in them. Only "e".

If you want to match on words that contain all of the strings you want to match against, you need to redo your logic:

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const test = ['e', 'i'];

const containsAll = words.filter( word => test.every (str => word.includes(str)))
const oneOf = words.filter( word => test.some (str => word.includes(str)))

console.log(containsAll);
console.log(oneOf);

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

6 Comments

Yeah, I just tested the ... with the above comment, however I've found a new issue. with simple characters, it seems to work fine, however, if I were to change it so var test = ['elite', 'limit'];, I get ["elite"] as the result. If I swap the order, so limit` is first, the result is ["limit"]. If I separate letters into separate characters, it sometimes works. if I were to enter var test = ['s', 'eli'] it appears to ignore "eli" and just returns anything that matches the "s"...
@Anthony That is TOTALLY different problem, actually, with its own problems. The first issue is about matching words that contain single characters. Your follow up question second question starts doing something very different. The main issue, though, is that you do not use the function as intended. That function only takes a single argument to match against, not multiple. It has an optional second argument, but that is just for the starting position. I will update the answer.
There. See full solutions
Well, this is probably more efficient than using regexes anyway. On that note, I read Friedl's "Mastering Regular Expressions" 15 years ago (while studying) and that made me very comfortable with various regex dialects. Very good investment of time and money just reading the first few chapters! But I am confused; did my last code not fix your issue? It will work with all kinds of strings, matching all sets of words containing either ALL of the strings or ANY of the strings (I supplied both versions).
I legit didn't see that when I commented, so either it was an edit or I'm just really intelligent /s (never said I was smart!) But seriously, thank you!
|

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.