0

First of all, please answer without giving me negative reputation, since I searched for this responsably before posting.

I want to filter an array of players, first the ones who has an "a" in their names, from these the ones who has an "e" and lastly from the ones who pass the two previously mentioned filters, the ones who start with z. I tried three methods individually no problems are all, but put together it does not filter, not even once.

const players2 = ["veron", "cambiasso", "batistuta", "zanetti", "Dalessandro", "zamerano", "cae", "cea", "cerini", "zii"];

const filter3 = (players) => (players.filter(
  player => {
    player.startsWith('z');
    return player;
  })

  .filter(
    player => {
      player.includes('e');

      return player;
    })

  .filter(
    player => {
      player.includes('a');

      return player;

    }
  ));


console.log("Argentineans players filtered are: " + filter3(players2));

5 Answers 5

1

filter()'s callback needs to return a boolean. Since you're always returning player, and that is always a truthy value, you're not filtering anything.

const players2 = ["veron", "cambiasso", "batistuta", "zanetti", "Dalessandro", "zamerano", "cae", "cea", "cerini", "zii"];

const filter3 = players => 
  players
    .filter(player => player.startsWith('z'))
    .filter(player => player.includes('e'))
    .filter(player => player.includes('a'));

console.log("Argentineans players filtered are: " + filter3(players2));

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

Comments

0

.filter() expects a boolean from the callback to determine if the current item should be included in the result. Whatever it gets will be converted to boolean.

Also, you don't need to filter multiple times. Just do one with &&. Otherwise you're iterating multiple times, which is more computationally expensive than necessary.

const players2 = ["veron", "cambiasso", "batistuta", "zanetti", "Dalessandro", "zamerano", "cae", "cea", "cerini", "zii"];

const filter3 = (players) => players.filter(
  player => 
    player.startsWith('z') &&
    player.includes('e') &&
    player.includes('a')
);


console.log("Argentineans players filtered are: " + filter3(players2));

Comments

0

It's not very efficient to chain numerous filter() when you can do it all in one. Each time you call it requires another iteration of the array returned from previous operation and another new array generated.

Use a && conditional to return a boolean.

const players2 = ["veron", "cambiasso", "batistuta", "zanetti", "Dalessandro", "zamerano", "cae", "cea", "cerini", "zii"];

const filter3 = (players) => players
  .filter(player => player.startsWith('z') && player.includes('e') &&player.includes('a') )
 

console.log("Argentineans players filtered are: " + filter3(players2));

Comments

0

The callback for filter should return a truthy value if the element should be kept and a falsey value otherwise. You should be returning the result of startsWith and includes instead of the player itself. Your code can be simplified to only one filter call as well:

const filter3 = (players) => players
  .filter(player => player.startsWith('z')&&player.includes('e')&&player.includes('a'));

const players2 = ["veron", "cambiasso", "batistuta", "zanetti", "Dalessandro", "zamerano", "cae", "cea", "cerini", "zii"];
const filter3 = (players) => players
  .filter(player => player.startsWith('z')&&player.includes('e')&&player.includes('a'));
console.log("Argentineans players filtered are: " + filter3(players2));

Comments

0

Thank you everybody for your replies.

I want to highlight the fact that I do know how to write a code to get the correct result, by writing each function individually and then call them with

console.log(function1(function2(function3(players2))));

The issue here for me is try and understand why arrow function callback is not working. After seeing your replies, this is getting much easier.

2 Comments

If one of these answers your question, you should mark it as your answer.
I mean you should click the checkbox next to the answer. That makes it officially the answer to this question.

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.