gpt4 book ai didi

javascript - 检查特定年龄

转载 作者:太空宇宙 更新时间:2023-11-04 03:03:07 25 4
gpt4 key购买 nike

我有一个 if 语句,如下所示:

function isMaleBetween(value, index, array) {
maleBetween = [];

array.forEach(function (element) {
if (element.active === filters.active &&
element.gender === filters.gender &&
element.age === filters.age.min(18) &&
element.age === filters.age.max(30)) {
console.log(element);
}
});
}

这是我的用户所在的类(class)。我已检查所有名称、ID 和过滤器都在这里:

export const UserConfig = {
test_case_1: {
getUsers: () => UserConfig.users,
filters: {
active: true
}
},
test_case_2: {
getUsers: () => UserConfig.users,
filters: {
active: true,
gender: 'female'
}
},
test_case_3: {
getUsers: () => UserConfig.users,
filters: {
active: false,
gender: 'male',
age: {
min: 18,
max: 30
}
}
},
users:[
{
"_id": "597c79908b3baf49d88204db",
"age": 17,
"name": "Mack Roach",
"gender": "male",
"active": false
},
{
"_id": "597c799049f0cf0a330d58dd",
"age": 22,
"name": "Guy Howe",
"gender": "male",
"active": true
},
{
"_id": "597c79908e284bdc450a4241",
"age": 22,
"name": "Marie Camacho",
"gender": "female",
"active": true
},
{
"_id": "597c79906b710574bc79c970",
"age": 21,
"name": "Hewitt Buckley",
"gender": "male",
"active": true
},
{
"_id": "597c7990ab221a09c6671ff5",
"age": 19,
"name": "Jeannine Beard",
"gender": "female",
"active": true
},
{
"_id": "597c7990f807c9cb122c52ea",
"age": 20,
"name": "Mooney Shepard",
"gender": "male",
"active": false
},
{
"_id": "597c7990fe0487381569b455",
"age": 18,
"name": "Norman Hardy",
"gender": "male",
"active": false
}
]
}

但是,当我运行此命令时,它不会循环遍历我的年龄最小 18 岁、最大 30 岁的男性数组。这个说法是错误的还是我遗漏了什么?

最佳答案

问题是您将它们当作函数来调用。您还需要使用比较运算符。

element.age >= filters.age.min && element.age <= filters.age.max

要在测试用例没有最小值或最大值的情况下为它们提供默认值,您可以使用 OR 运算符:

element.age >= (filters.age.min || 18) && element.age <= (filters.age.max || 30)

关于javascript - 检查特定年龄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49020213/

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