我正在使用array.filter过滤以下数组
let array = [[75, -5750, 115],
[76, -5750, 115],
[93, -5750, 115],
[94, -5750, 115],
[95, -5750, 115],
[96, -5750, 115],
[97, -5750, 115],
[98, -5750, 115],
[99, -5750, 115]]
var lucky = array.filter(a=>a[0]>75); //this works
console.log(lucky);
我想要实现
var lucky = array.filter(a=>98>a[0]>75); //apply two condition //it returns whole array in this case
如何实现????
答案 0 :(得分:2)
好像您想要的是logical and operator。像这样:
array.filter(a => 98 > a[0] && a[0] > 75);
答案 1 :(得分:1)
仅使用&&
仅在满足两个条件的情况下返回true
let array = [
[75, -5750, 115],
[76, -5750, 115],
[93, -5750, 115],
[94, -5750, 115],
[95, -5750, 115],
[96, -5750, 115],
[97, -5750, 115],
[98, -5750, 115],
[99, -5750, 115]
]
var lucky = array.filter(a => a[0] > 75 && a[0] < 98);
console.log(lucky);
.filter()
接受一个函数作为参数。在这种情况下,将与
function(element){
return element[0] > 75 && element[0] < 98;
}
答案 2 :(得分:1)
**您需要使用逻辑运算符来组合两个条件
如果您希望所有条件都必须使用&
AND运算符。
如果您希望consiton是可选的,则可以使用|
OR运算符。
let array = [[75, -5750, 115],
[76, -5750, 115],
[93, -5750, 115],
[94, -5750, 115],
[95, -5750, 115],
[96, -5750, 115],
[97, -5750, 115],
[98, -5750, 115],
[99, -5750, 115]]
var lucky = array.filter(a=>a[0]>75 && a[0]<98); //this works
console.log(lucky);