gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:11:17 24 4
gpt4 key购买 nike

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

当前工作输出(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(也与您的函数名称相同),但您使用的是 averageRating评论。您应该声明以下变量:

var averageRating,
reviews;

关于javascript - 通过 javascript 对象属性值过滤 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46942987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com