[Vuejs]-What is the best way to write an array sorting and filtering function with multiple parameters?

0👍

There’s a lot of duplicated logic here. The first step to making this more efficient would be cleaning up the if statements. For example you have:

if (genre == "все" && isChild && isLocalMultiplayer) {
...
} else if (isChild && isLocalMultiplayer) {

Some of these conditions can be checked once with a nested if:

if (isChild && isLocalMultiplayer) {
    if (genre == "все" {
    ...
    }
}

Additionally the returns could be simplified for the same reason. For example, this return

      return (
    (game.category == "ps5" &&
      isChild &&
      game.isLocalMultiplayer &&
      game.title.toLowerCase().includes(query)) ||
    (game.category == "ps5" &&
      isChild &&
      game.isLocalMultiplayer &&
      game.genre.includes(query)) ||
    (game.category == "ps5" &&
      isChild &&
      game.isLocalMultiplayer &&
      game.tag.includes(query))
  );

Could be simplified to:

 return (
    game.category == "ps5" &&
    isChild &&
    game.isLocalMultiplayer && 
   (game.title.toLowerCase().includes(query) || 
    game.genre.includes(query) ||
    game.tag.includes(query))
);

I hope this will help you get to the bottom of the other issues you’re seeing here, good luck!

Leave a comment