简体   繁体   English

通过javascript对象属性值过滤JSON响应

[英]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. 我正在尝试创建2个简单的过滤器,以便仅显示或输出通过这2个过滤器的“ healthyMeal”。

Current working output (before 2 filter): 当前工作输出(2个滤波器之前):

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

Attempt (trying to add 2 filters). 尝试(尝试添加2个过滤器)。

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? 我如何循环遍历“ averageRating ”和“ reviews ”的所有healthyMeals属性值,并且如果它们通过了2个过滤器测试,那么,然后才显示? If not message 'Not found' displays. 如果没有,则显示“未找到”消息。

You have declared your variables as checkReview and checkRating (which are also same as your function names) but you are using averageRating and reviews . 您已声明的变量作为checkReviewcheckRating (这也与您的函数名),但使用的是averageRatingreviews You should declare the below variables: 您应该声明以下变量:

var averageRating,
    reviews;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM