在某个年龄之间检查

时间:2018-02-28 00:37:31

标签: javascript node.js jasmine karma-runner project

我有一个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);
           }
        });
    }

这是我的用户所在的班级。我已经检查过所有的名称,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年。这个陈述是错的还是我错过了什么?

3 个答案:

答案 0 :(得分:3)

问题是你把它们称为功能。您还需要使用比较运算符。

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

如果测试用例没有最小值或最大值,为了给它们提供默认值,可以使用OR运算符:

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

答案 1 :(得分:1)

您可以使用filter

let array=[ { "_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": 32, "name": "Marie Camacho", "gender": "female", "active": true }];
let filtered_array=array.filter(a=>a.age >=18 &&  a.age<=30 && a.gender==='male');

filtered_array.forEach(function (element) {
    console.log(element);
       
    });

答案 2 :(得分:0)

如果您尝试使用选择对象的相关方式,这是另一个视角。

var ge = UserConfig.users.filter((object)=> object.gender === 'male')
                         .filter((male)=> male.age >= 18 && male.age <= 30)

console.log(ge);

通过男性过滤,然后过滤他们的年龄。到最后的那些,将放在全新的列表中,您可以记录它以查看它的作用。