# js多条件查询及分页
if (0) { // 表达式为数值0、null、undefined、false、空字符串时,条件不成立
// 不执行
}
var aim = [
{name:'Anne', age: 0, gender:'female'},
{name:'Leila', age: 16, gender:'female'},
{name:'Jay', age: 19, gender:'male'},
{name:'Mark', age: 40, gender:'male'}
]
let searchProps = {
name: {
operator: "LIKE",
valueType: "STRING",
value: null,
},
age: {
operator: "EQUAL",
valueType: "STRING",
value: 0,
}
}
function filterBySearch(aim, searchProps) {
let arr = aim.filter(item => {
let result = true;
for(var prop in searchProps) {
if(searchProps[prop].value == 0) {
searchProps[prop].value = '0'
}
if(searchProps[prop].value) {
if(searchProps[prop].operator == 'LIKE') {
result = result && (item[prop].indexOf(searchProps[prop].value) > -1)
} else {
result = result && (item[prop] == searchProps[prop].value)
}
} else {
result = result && true
}
}
return result;
})
return arr
//过滤后数组
let filterArr = filterBySearch(aim, searchProps);
let newArr = Object.assign([], filterArr);
//分页
let pageNum = 10;
let pageSize = 1;
let start = pageNum * pageSize - pageSize;
let end = pageNum * pageSize;
let newData = newArr.slice(start, end);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
← 快速删除node_modules FAQ →