繁体   English   中英

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

[英]filter JSON response by javascript object property value

我目前正在提取以下响应输出; 我正在尝试创建2个简单的过滤器,以便仅显示或输出通过这2个过滤器的“ healthyMeal”。

当前工作输出(2个滤波器之前):

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

尝试(尝试添加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()
    }
  })

以上两个带有注释的属性会破坏应用程序,并且在未注释时不起作用。

我如何循环遍历“ averageRating ”和“ reviews ”的所有healthyMeals属性值,并且如果它们通过了2个过滤器测试,那么,然后才显示? 如果没有,则显示“未找到”消息。

您已声明的变量作为checkReviewcheckRating (这也与您的函数名),但使用的是averageRatingreviews 您应该声明以下变量:

var averageRating,
    reviews;

暂无
暂无

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

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