0

in my vue js code below i have questions came from API with their categories all saved in questions array, what i want to achieve is when any of those categories clicked it should filter the questions that related to those categories only, but in my code it shows only single question with its category... like it's not showing all related questions with that clicked category .. any help please?

  selectedAnswer(index) {
      this.selectedIndex = index;
      this.questions = this.questions.filter((question) =>
        question.categories.includes(index)
      );
      console.log(index);
    },

4
  • I don't know what you wanna do, be clear. Commented Jan 2, 2021 at 14:57
  • Show questions array and how do you want to filter that array. What is the filter criteria? Commented Jan 2, 2021 at 14:59
  • i have questions came from API with their categories all saved in questions array, what i want to achieve is when any of those categories clicked it should filter the questions that related to those categories only, in my code it shows only single question with its category... like it's not showing all related questions with that clicked category Commented Jan 2, 2021 at 15:17
  • It is hard to answer if I don't know the questions array. It would be helpful if you can show some demo code. You could use codesandbox if you want. Happy to help. Commented Jan 2, 2021 at 15:26

1 Answer 1

1

As I try to understand your code, you need a primitive type(string, number, boolean) to filter out of your array. Otherwise, you can also try iterate inside of your filter since you comparing the objects.

    selectedAnswer(index) {
      this.selectedIndex = index;
      this.questions = this.questions.filter((question) =>
        question.categories.some((cat) => cat._id === index)
      );
    },

Note that 'index' is selected id of category.

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

Comments

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.