[英]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個過濾器測試,那么,然后才顯示? 如果沒有,則顯示“未找到”消息。
您已聲明的變量作為checkReview
和checkRating
(這也與您的函數名),但使用的是averageRating
和reviews
。 您應該聲明以下變量:
var averageRating,
reviews;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.