jeudi 4 avril 2019

How to select a few random id values from an array based on a matching argument?

I have an array of 700 question objects that look like this:

[
  {
    _id: "5ca3216579a592b0910f70d4"
    question: "Who wrote Twilight series of novels?"
    answer: "Stephenie Meyer"
    category: "Art and Literature"
  },
  ...
]

I also have an array of 3 selected question categories that look like this:

["Art and Literature", "Music", "History"] 

What I basically need is 4 random questions of each question category. So:

  • 4 random questions of Art and Literature
  • 4 random questions of Music
  • 4 random questions of History

Ideally I think it would be nice to have the 12 random question id's set in a new array based these 3 categories so I could send them to my database.

How is this to be done?




Aucun commentaire:

Enregistrer un commentaire