如何过滤具有多个条件的数组?
var users = [
{ user: "barney", age: 36, active: true },
{ user: "fred", age: 39, active: false },
{ user: "fred", age: 40, active: false },
{ user: "fred", age: 41, active: false },
{ user: "fred", age: 42, active: false },
{ user: "fred", age: 43, active: false },
]
_.filter(users, function (o) {
return o.age !== 40 || o.age !== 41 || o.age !== 42
})
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>
答案 0 :(得分:0)
谓词功能有一个小问题
o.age !== 40 || o.age !== 41 || o.age !== 42
通过每个对象,因为年龄只有一个值,所以检查它是否不同于3个不同的值绝对是正确的
以下修补程序可以为您提供帮助
var users = [
{ user: "barney", age: 36, active: true },
{ user: "fred", age: 39, active: false },
{ user: "fred", age: 40, active: false },
{ user: "fred", age: 41, active: false },
{ user: "fred", age: 42, active: false },
{ user: "fred", age: 43, active: false },
]
const res = _.filter(users, function (o) {
return ![40, 41, 42].includes(o.age)
})
console.log(res)
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>