Lieutenant Dan
Lieutenant Dan

Reputation: 8264

filter JSON response by javascript object property value

I'm currently pulling the below response outputs; I am trying to create 2 simple filters, so that only 'healthyMeals' that pass these 2 filters, will display, or output.

Current working output (before 2 filter):

{
  "healthyMeals" : [
    {
      "id": 310,
      "avRating": 2.2,
      "name": "Peanut butter soup",
      "expireDate": "12-02-2017",
      "difficulty": "easy",
      "reviews": 999
    },

Attempt (trying to add 2 filters).

this.getHealthy = function(res, req) {

  var checkReview;
  var checkRating;

  function checkRating() {
   if (averageRating > 4.1){
        res.res.json({
        "message" : "pass",
        });         
      } 
   else {
        res.res.json({
        "message" : "Could not find meal",
        });         
      } 
  };
  function checkReview() {
    if (reviews >= 5){
        res.res.json({
        "message" : "pass",
        });
      } 
    else {
        res.res.json({
        "message" : "Could not find meal",
        });         
      } 
  };   

  db.Meals.findAll({
    where: {
      id : givenId,
 //   averageRating : checkRating(),
 //   reviews : checkReview()
    }
  })

The above two properties with comments, breaks app and doesn't work when uncommented out.

How could I loop through all healthyMeals property values of 'averageRating' and 'reviews' and if they pass the 2 filter tests, then, and only then display? If not message 'Not found' displays.

Upvotes: 0

Views: 65

Answers (1)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48067

You have declared your variables as checkReview and checkRating (which are also same as your function names) but you are using averageRating and reviews. You should declare the below variables:

var averageRating,
    reviews;

Upvotes: 1

Related Questions